Skip to content

Commit 8f53c7b

Browse files
committed
Facebook Hacker Cup 2017 Round 3
1 parent c8d7c4f commit 8f53c7b

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/**
5+
* Facebook Hacker Cup 2017 Round 3
6+
* Problem A. Salient Strings
7+
*/
8+
public class Main {
9+
10+
public String solve(Scanner scanner) throws Exception {
11+
int n=scanner.nextInt();
12+
char[] s=new char[n];
13+
int[] a=new int[n+1], p=new int[n];
14+
for (int i=0;i<n;i++) {
15+
int x=scanner.nextInt()-1;
16+
a[i]=x; p[x]=i;
17+
}
18+
a[n]=-1;
19+
s[p[0]]='A';
20+
for (int i=1;i<n;i++) {
21+
if (a[p[i]+1]>a[p[i-1]+1])
22+
s[p[i]]=s[p[i-1]];
23+
else {
24+
if (s[p[i-1]]=='Z') return "-1";
25+
s[p[i]]=(char)(s[p[i-1]]+1);
26+
}
27+
}
28+
return new String(s);
29+
}
30+
31+
public static void main(String[] args) throws Exception {
32+
System.setIn(new FileInputStream("input.txt"));
33+
System.setOut(new PrintStream("output.txt"));
34+
Scanner scanner=new Scanner(System.in);
35+
int times=Integer.parseInt(scanner.nextLine());
36+
long start=System.currentTimeMillis();
37+
for (int t=1;t<=times;t++) {
38+
try {
39+
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
40+
}
41+
catch (Throwable e) {
42+
System.err.println("ERROR in case #"+t);
43+
e.printStackTrace();
44+
}
45+
}
46+
long end=System.currentTimeMillis();
47+
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
48+
49+
}
50+
}
+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/**
5+
* Facebook Hacker Cup 2017 Round 3
6+
* Problem B. Sluggish Security
7+
*/
8+
public class Main {
9+
10+
private static final long MOD = 1000000007L;
11+
12+
private static long[] x, y;
13+
14+
public String solve(Scanner scanner) throws Exception {
15+
calMod();
16+
int n=scanner.nextInt();
17+
long[] a=input(scanner, n), b=input(scanner, n);
18+
19+
int ai=0, bi=0;
20+
long ans=1;
21+
22+
while (ai<n || bi<n) {
23+
24+
////// CASE 1: X,X
25+
if (ai<n && bi<n && a[ai]==b[bi]) {
26+
ai++; bi++;
27+
}
28+
29+
////// CASE 2: XY,YX
30+
else if (ai+1<n && bi+1<n && a[ai]==b[bi+1] && a[ai+1]==b[bi]) {
31+
ans*=2; ans%=MOD;
32+
ai+=2; bi+=2;
33+
}
34+
35+
////// CASE 3: XYX,Y
36+
else if (ai+2<n && bi<n && a[ai]==a[ai+2] && a[ai+1]==b[bi]) {
37+
ans*=2; ans%=MOD;
38+
ai+=3; bi++;
39+
}
40+
41+
////// CASE 4: X,YXY
42+
else if (bi+2<n && ai<n && b[bi]==b[bi+2] && b[bi+1]==a[ai]) {
43+
ans*=2; ans%=MOD;
44+
ai++; bi+=3;
45+
}
46+
47+
////// CASE 5: XX|XYXY|...
48+
else {
49+
int al = 0, bl = 0;
50+
while (true) {
51+
if (ai+1<n && a[ai]==a[ai+1]) {
52+
ai+=2;
53+
al++;
54+
}
55+
else if (ai+3<n && a[ai]==a[ai+2] && a[ai+1]==a[ai+3]) {
56+
ai+=4;
57+
al++;
58+
}
59+
else break;
60+
}
61+
while (true) {
62+
if (bi+1<n && b[bi]==b[bi+1]) {
63+
bi+=2;
64+
bl++;
65+
}
66+
else if (bi+3<n && b[bi]==b[bi+2] && b[bi+1]==b[bi+3]) {
67+
bi+=4;
68+
bl++;
69+
}
70+
else break;
71+
}
72+
73+
// Not IMPOSSIBLE
74+
if (al==0 && bl==0) return "0";
75+
76+
ans=ans*x[al+bl]%MOD*y[al]%MOD*y[bl]%MOD;
77+
}
78+
}
79+
return String.valueOf(ans);
80+
}
81+
82+
private long[] input(Scanner scanner, int n) {
83+
long[] a=new long[n], da=new long[n];
84+
a[0]=scanner.nextLong();
85+
int k=scanner.nextInt(), index=0;
86+
for (int i=0;i<k;i++) {
87+
int r=scanner.nextInt(), l=scanner.nextInt();
88+
ArrayList<Long> list=new ArrayList<>();
89+
for (int j=0;j<l;j++) list.add(scanner.nextLong());
90+
for (int j=0;j<r;j++) {
91+
for (long u: list) {
92+
a[index+1]=a[index]+u;
93+
index++;
94+
}
95+
}
96+
}
97+
return a;
98+
}
99+
100+
private static void calMod() {
101+
if (x!=null) return;
102+
x=new long[5000000]; y=new long[5000000];
103+
104+
// x
105+
x[0]=1;
106+
y[0]=1;
107+
for (int i=1;i<5000000;i++) {
108+
x[i]=x[i-1]*i%MOD;
109+
y[i]=quickPow(x[i], MOD-2);
110+
}
111+
}
112+
113+
// x^n%p
114+
private static long quickPow(long x, long n) {
115+
if (n==0) return 1;
116+
if (n==1) return x;
117+
long v=quickPow(x, n/2);
118+
return v*v%MOD*(n%2!=0?x:1)%MOD;
119+
}
120+
121+
public static void main(String[] args) throws Exception {
122+
System.setIn(new FileInputStream("input.txt"));
123+
System.setOut(new PrintStream("output.txt"));
124+
Scanner scanner=new Scanner(System.in);
125+
int times=Integer.parseInt(scanner.nextLine());
126+
long start=System.currentTimeMillis();
127+
for (int t=1;t<=times;t++) {
128+
try {
129+
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
130+
}
131+
catch (Throwable e) {
132+
System.err.println("ERROR in case #"+t);
133+
e.printStackTrace();
134+
}
135+
}
136+
long end=System.currentTimeMillis();
137+
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
138+
139+
}
140+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ ALL JAVA SOLUTIONS HERE!
122122
- [Qualification Round](Facebook%20Hacker%20Cup/2017/Qualification%20Round): A/B/C
123123
- [Round1](Facebook%20Hacker%20Cup/2017/Round1): A/B/C
124124
- [Round2](Facebook%20Hacker%20Cup/2017/Round2): A/D
125+
- [Round3](Facebook%20Hacker%20Cup/2017/Round3): A/B
125126

126127
### Other Contests
127128
To be continued...

0 commit comments

Comments
 (0)