File tree 2 files changed +60
-0
lines changed
2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Idea:
3
+ - We can solve this problem using DP.
4
+ - In each step in the DP try to take each door using its prbability.
5
+ - If the door takes you outside, then add its hours * its probability
6
+ to the result, otherwise if the door bring you back add its hours *
7
+ its probability and go from it to new recursion call, but the multiply
8
+ the returned value with the current door probability.
9
+ */
10
+
11
+ #include < bits/stdc++.h>
12
+
13
+ using namespace std ;
14
+
15
+ int const N = 1e2 + 1 ;
16
+ int t, n, x[N];
17
+ double p[N], dp[N * N];
18
+
19
+ double rec (int cur) {
20
+ if (cur > N * N)
21
+ return 0 ;
22
+
23
+ double &ret = dp[cur];
24
+ if (ret == ret)
25
+ return ret;
26
+ ret = 0 ;
27
+
28
+ for (int i = 0 ; i < n; ++i)
29
+ if (x[i] > 0 )
30
+ ret += 1.0 * x[i] * p[i];
31
+ else {
32
+ ret += 1.0 * -x[i] * p[i] + p[i] * rec (cur + 1 );
33
+ }
34
+
35
+ return ret;
36
+ }
37
+
38
+ int main () {
39
+ int kase = 0 ;
40
+ scanf (" %d" , &t);
41
+ while (t-- != 0 ) {
42
+ memset (dp, -1 , sizeof dp);
43
+
44
+ bool ok = false ;
45
+ scanf (" %d" , &n);
46
+ for (int i = 0 ; i < n; ++i) {
47
+ scanf (" %d %lf" , x + i, p + i);
48
+ if (x[i] > 0 )
49
+ ok = true ;
50
+ }
51
+
52
+ if (!ok)
53
+ printf (" Case %d: God! Save me\n " , ++kase);
54
+ else
55
+ printf (" Case %d: %.2lf\n " , ++kase, rec (0 ));
56
+ }
57
+
58
+ return 0 ;
59
+ }
Original file line number Diff line number Diff line change 118
118
- [ 10721 - Bar Codes] ( https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1662 )
119
119
- [ 10731 - Test] ( https://uva.onlinejudge.org/index.php?option=onlinejudge&Itemid=99999999&page=show_problem&problem=1672 )
120
120
- [ 10765 - Doves and bombs] ( https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1706 )
121
+ - [ 10777 - God! Save me] ( https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1718 )
121
122
- [ 10789 - Prime Frequency] ( https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1730 )
122
123
- [ 10804 - Gopher Strategy] ( https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1745 )
123
124
- [ 10815 - Andy's First Dictionary] ( https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1756 )
You can’t perform that action at this time.
0 commit comments