-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathC.java
68 lines (63 loc) · 2.09 KB
/
C.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.*;
import java.util.*;
/**
* Facebook Hacker Cup 2017 Qualification Round
* Problem C. Fighting the Zombie
*/
public class Main {
public String solve(Scanner scanner) {
int k=scanner.nextInt(), n=scanner.nextInt();
double ans=0;
while (n-->0) {
int kk=k, index;
String x=scanner.next();
if ((index=x.indexOf('+'))>=0) {
kk-=Integer.parseInt(x.substring(index+1));
x=x.substring(0, index);
}
else if ((index=x.indexOf('-'))>=0) {
kk+=Integer.parseInt(x.substring(index+1));
x=x.substring(0, index);
}
String[] xx=x.split("d");
ans=Math.max(ans, calculate(Integer.parseInt(xx[0]), Integer.parseInt(xx[1]), kk));
}
return String.format("%.9f", ans);
}
private double calculate(int x, int y, int k) {
if (k<=x) return 1;
if (k>x*y) return 0;
double[][] dp=new double[x+1][k];
dp[0][0]=1;
for (int i=1;i<=x;i++) {
for (int j=0;j<k;j++) {
for (int u=1;u<=y;u++) {
if (j>=u)
dp[i][j]+=dp[i-1][j-u];
}
dp[i][j]/=y;
}
}
double ans=1;
for (int j=0;j<k;j++)
ans-=dp[x][j];
return ans;
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=Integer.parseInt(scanner.nextLine());
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
try {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
catch (Throwable e) {
System.err.println("ERROR in case #"+t);
e.printStackTrace();
}
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}