博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Help Me Escape ZOJ - 3640
阅读量:4577 次
发布时间:2019-06-08

本文共 10637 字,大约阅读时间需要 35 分钟。

Background

    If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at the door. And unto thee shall be his desire, and thou shalt rule over him. 

    And Cain talked with Abel his brother: and it came to pass, when they were in the field, that Cain rose up against Abel his brother, and slew him. 
    And the LORD said unto Cain, Where is Abel thy brother? And he said, I know not: Am I my brother's keeper? 
    And he said, What hast thou done? the voice of thy brother's blood crieth unto me from the ground. 
    And now art thou cursed from the earth, which hath opened her mouth to receive thy brother's blood from thy hand; 
    When thou tillest the ground, it shall not henceforth yield unto thee her strength; a fugitive and a vagabond shalt thou be in the earth.

—— Bible Chapter 4

Now Cain is unexpectedly trapped in a cave with N paths. Due to LORD's punishment, all the paths are zigzag and dangerous. The difficulty of the ith path is ci.

Then we define f as the fighting capacity of Cain. Every day, Cain will be sent to one of the N paths randomly.

Suppose Cain is in front of the ith path. He can successfully take ti days to escape from the cave as long as his fighting capacity f is larger than ci. Otherwise, he has to keep trying day after day. However, if Cain failed to escape, his fighting capacity would increase ci as the result of actual combat. (A kindly reminder: Cain will never died.)

As for ti, we can easily draw a conclusion that ti is closely related to ci. Let's use the following function to describe their relationship:

 

 

After D days, Cain finally escapes from the cave. Please output the expectation of D.

Input

The input consists of several cases. In each case, two positive integers N and f (n ≤ 100, f ≤ 10000) are given in the first line. The second line includes N positive integers ci (ci ≤ 10000, 1 ≤ i ≤ N)

Output

For each case, you should output the expectation(3 digits after the decimal point).

Sample Input

3 11 2 3

Sample Output

6.889 给出 n 条路,然后每条路有一个c[i]的困难值 和 t[i] 的逃脱天数,一个人的战斗力是 f ,当战斗力大于 c[i] 时, 花t[i]天逃出去,否则下一天随机到另一个地方, 问你这个人期望的走出去的天数是几天。 一开始考虑 dp[i][j] 表示从第 i 个位置出发,战斗力为 j 时的期望逃脱天数。这样可以得到: 1. j > c[i], dp[i][j] = t[i] 2. j <= c[i], dp[i][j] = Σ(dp[k][j+c[i]] + 1) / n 然后去枚举开始的位置,并加上记忆化搜索,感觉是可以的,但是这样做会 T 。
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 #include  49 #include 
50 #include
51 #include
52 #include
53 #include
54 #include
55 #include
56 #include
57 #include
58 #include
59 #include
60 #include
61 #include
62 #include
63 #define lowbit(x) x & (-x) 64 #define mes(a, b) memset(a, b, sizeof a) 65 #define fi first 66 #define se second 67 #define pii pair
68 #define INOPEN freopen("in.txt", "r", stdin) 69 #define OUTOPEN freopen("out.txt", "w", stdout) 70 71 typedef unsigned long long int ull; 72 typedef long long int ll; 73 const int maxn = 1e2 + 10; 74 const int maxm = 2e4 + 10; 75 const int mod = 1e9 + 7; 76 const ll INF = 1e18 + 100; 77 const int inf = 0x3f3f3f3f; 78 const double pi = acos(-1.0); 79 const double eps = 1e-8; 80 using namespace std; 81 82 int n, m; 83 int cas, tol, T; 84 85 int c[maxn]; 86 int t[maxn]; 87 double dp[maxn][maxm]; 88 89 void init() { 90 mes(c, 0); 91 mes(t, 0); 92 mes(dp, 0); 93 } 94 95 void dfs(int id, int f) { 96 if(dp[id][f]) return ; 97 if(f > c[id]) { 98 dp[id][f] = 1.0 * t[id]; 99 return ;100 }101 double ans = 0.0;102 for(int i=1; i<=n; i++) {103 dfs(i, f+c[id]);104 ans += dp[i][f+c[id]] + 1.0;105 }106 dp[id][f] = ans / n;107 }108 109 int main() {110 while(~scanf("%d%d", &n, &m)) {111 init();112 for(int i=1; i<=n; i++) {113 scanf("%d", &c[i]);114 t[i] = (int)((1.0 + sqrt(5.0)) / 2 * c[i] * c[i]);115 }116 double ans = 0.0;117 for(int i=1; i<=n; i++) {118 dfs(i, m);119 ans += dp[i][m];120 }121 printf("%.3f\n", ans / n);122 }123 return 0;124 }
TLE
观察方程发现 dp[i][j] 的 i 每次都要去重复枚举,花费了太多的时间,那么想办法优化掉一维。 dp[i] 表示战斗力为 i 时的期望天数。 然后再去搜索每条路 1. i > c[j], dp[i] += t[j] 2. i <= c[j], dp[i] += dp[i + c[j]] + 1 然后在记忆化搜索,就能过了....
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 #include  49 #include 
50 #include
51 #include
52 #include
53 #include
54 #include
55 #include
56 #include
57 #include
58 #include
59 #include
60 #include
61 #include
62 #include
63 #define lowbit(x) x & (-x) 64 #define mes(a, b) memset(a, b, sizeof a) 65 #define fi first 66 #define se second 67 #define pii pair
68 #define INOPEN freopen("in.txt", "r", stdin) 69 #define OUTOPEN freopen("out.txt", "w", stdout) 70 71 typedef unsigned long long int ull; 72 typedef long long int ll; 73 const int maxn = 1e2 + 10; 74 const int maxm = 1e5 + 10; 75 const int mod = 1e9 + 7; 76 const ll INF = 1e18 + 100; 77 const int inf = 0x3f3f3f3f; 78 const double pi = acos(-1.0); 79 const double eps = 1e-8; 80 using namespace std; 81 82 int n, m; 83 int cas, tol, T; 84 85 int c[maxn]; 86 int t[maxn]; 87 double dp[maxm]; 88 89 void init() { 90 mes(c, 0); 91 mes(t, 0); 92 mes(dp, 0); 93 } 94 95 void dfs(int f) { 96 if(dp[f]) 97 return ; 98 double ans1 = 0.0; 99 double ans2 = 0.0;100 for(int i=1; i<=n; i++) {101 if(f > c[i]) {102 ans1 += 1.0 * t[i];103 } else {104 dfs(f+c[i]);105 ans2 += dp[f+c[i]];106 ans2 += 1.0;107 }108 }109 dp[f] = (ans1 + ans2) / n;110 }111 112 int main() {113 while(~scanf("%d%d", &n, &m)) {114 init();115 for(int i=1; i<=n; i++) {116 scanf("%d", &c[i]);117 t[i] = (int)((1.0 + sqrt(5.0)) / 2 * c[i] * c[i]);118 }119 dfs(m);120 printf("%.3f\n", dp[m]);121 }122 return 0;123 }
AC

 

转载于:https://www.cnblogs.com/Jiaaaaaaaqi/p/10100200.html

你可能感兴趣的文章
1-4-03:奇偶数判断
查看>>
【SQL Server备份恢复】提高SQL Server备份速度
查看>>
命令行简介(附加参考资料)
查看>>
从0开始整合SSM框架-1.mybatis
查看>>
移位操作的疑问
查看>>
UILabel常用属性小结
查看>>
gitlab 邮件服务器配置
查看>>
Python 循环语句(while, for)
查看>>
深入理解JavaScript原型链
查看>>
LinearGradient类来实现图片的渐变效果
查看>>
Golang关键字—— if/else
查看>>
数据清洗
查看>>
PHP&MySQL(三)——数组
查看>>
各种语法解释及用法
查看>>
GPS.NET 和 GeoFramework开源了
查看>>
汇编:采用址表的方法编写程序实现C程序的switch功能
查看>>
AtiveMQ初次连接的 http error:503 连接错误 Prolem accessing /.Reason : Service Unavailable...
查看>>
OFO和摩拜共享单车
查看>>
数据适配 DataAdapter对象
查看>>
有序列表ol和定义列表dl,dt,dd
查看>>