zoj 3640 Help Me Escape (概率dp 递归求期望)
Help Me Escape
Time Limit: 2 Seconds Memory Limit: 32768 KB
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 1
- 1 2 3
Sample Output
- 6.889
题意:
有一个吸血鬼被困了,有n条路可以逃出去,每条路有一个难度c[],他初始的战斗力是f,对于第i条路,若f > c[i]他花t[i]天就能出去,否则,他就停留一天,同时战斗力增加c[i]然后再选一条路走出去,他走每条路的概率是相同的。问他逃出去的天数的期望。
注意t[i]是整数。
分析:
d[i]表示战斗力为 i 的时候的逃出去的期望。
用递归的好处是思路比较清楚,就是每次按照概率加 什么时候能逃出去的期望。
这个题也有逆推 递推的做法。
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <queue>
- #include <cmath>
- #include <algorithm>
- #define LL __int64
- const int maxn = +;
- using namespace std;
- double d[+], t[maxn]; //注意数组开到了2倍,因为终态可能是20000.
- int c[maxn], n;
- double dfs(int f)
- {
- if(d[f]>) return d[f]; //相当于剪枝,已经计算过的不再计算
- for(int i = ; i < n; i++)
- {
- int tmp = (int)t[i];
- if(f>c[i])
- d[f] += 1.0/n*(double)tmp;
- else
- d[f] += 1.0/n*(1.0+dfs(f+c[i]));
- }
- return d[f];
- }
- int main()
- {
- int f, i;
- while(~scanf("%d%d", &n, &f))
- {
- for(i = ; i < n; i++)
- {
- scanf("%d", &c[i]);
- t[i] = (double)c[i]*c[i]*1.0*(1.0+sqrt(5.0))/2.0;
- }
- memset(d, , sizeof(d));
- printf("%.3lf\n", dfs(f));
- }
- return ;
- }
zoj 3640 Help Me Escape (概率dp 递归求期望)的更多相关文章
- zoj 3640 Help Me Escape 概率DP
记忆化搜索+概率DP 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...
- POJ 2096 Collecting Bugs (概率DP,求期望)
Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu ...
- ZOJ 3329 One Person Game 【概率DP,求期望】
题意:有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和. 当分数大于n时结束.求游戏的期望步数.初始分数为0 设dp[i]表示达到 ...
- ZOJ 3329 One Person Game(概率DP,求期望)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题目大意: 有三个骰子,分别有K1,K2,K3个面,一次投掷可以得到三个 ...
- BZOJ2554 color 【概率DP】【期望DP】
题目分析: 好题. 一开始看错题了,以为是随机选两个球,编号在前的染编号在后的. 但这样仍然能获得一些启发,不难想到可以确定一个颜色,剩下的颜色是什么就无关了. 那么答案就是每种颜色的概率乘以期望.概 ...
- 概率dp——逆推期望+循环迭代zoj3329
首先要推出dp[i]的期望方程,会发现每一项都和dp[0]相关, 那我们将dp[i]设为和dp[0]有关的式子dp[i]=a[i]*dp[0]+b[i],然后再回代到原来的期望方程里 然后进行整理,可 ...
- ZOJ 3640 Help Me Escape:期望dp
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3640 题意: 有一个吸血鬼被困住了,他要逃跑... 他面前有n条 ...
- BZOJ 3640: JC的小苹果 [概率DP 高斯消元 矩阵求逆]
3640: JC的小苹果 题意:求1到n点权和\(\le k\)的概率 sengxian orz的题解好详细啊 容易想到\(f[i][j]\)表示走到i点权为j的概率 按点权分层,可以DP 但是对于\ ...
- zoj 3329 One Person Game 概率DP
思路:这题的递推方程有点麻烦!! dp[i]表示分数为i的期望步数,p[k]表示得分为k的概率,p0表示回到0的概率: dp[i]=Σ(p[k]*dp[i+k])+dp[0]*p0+1 设dp[i]= ...
随机推荐
- 九度OJ 1008:最短路径问题 (最短路)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8064 解决:2685 题目描述: 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费 ...
- 九度OJ 1044:Pre-Post(先序后序) (n叉树、递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:701 解决:398 题目描述: We are all familiar with pre-order, in-order and post- ...
- runsv
runsv(8) manual page http://smarden.org/runit/runsv.8.html Name runsv - starts and monitors a servic ...
- 504 Gateway Timeout Error 502 Bad Gateway
总结 1. 502没有收到相应,或者收到了但不及时? cannot get a response in time 540收到了无效的响应 received an invalid response fr ...
- mysql 创建用户与授权
权限管理 我们都知道,最高权限管理者是 root 用户 , 它拥有着最高的权限操作,包括 : select(查询) ,update(修改) , delete(删除,有事没事都不要用这个,反正也不能给这 ...
- Java中的内存泄漏
[转]介绍Java中的内存泄漏 1. 什么是内存泄漏? 内存泄漏的定义:对象已经没有被应用程序使用,但是垃圾回收器没办法移除它们,因为还在被引用着. 要想理解这个定义,我们需要先了解一下对象在内存中的 ...
- 20145239杜文超 《Java程序设计》第1周学习总结
20145239<Java程序设计>第1周学习总结 教材学习内容总结 第一周. 通过教材简单的了解了java的历史.因为之前看过视频,所以有一个大致明了的认识. 识记了Java三大平台:J ...
- debian7配置
输入法: apt-get install ibus ibus-pinyin 并执行ibus-setup进行配置,首选项->输入法->中文,然后按添加按钮即可. 软件开发基本软件:apt-g ...
- 本地Ubuntu16搭建Seafile
本地搭建Seafile 1.下载 2.解压 3.创建目录 mySeafile 4.将解压包放入mySeafile中 5.创建installed 将压缩包放入installed 安装准备工作 pytho ...
- HDU 4336 Card Collector:状压 + 期望dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意: 有n种卡片(n <= 20). 对于每一包方便面,里面有卡片i的概率为p[i],可 ...