ZOJ3640 概率DP
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[],师傅初始战斗力F0。每天,师傅会随机选择一个妖怪决斗,如果打得赢Ft>c[],就可以逃出去,逃出去要t[]天;否则,师傅会拿出秘籍练功,将自己变强,F(t+1)=Ft+c[],第二天寻找下一次机会。问师傅逃脱所用天数的数学期望。
思路:
设dp[F]是战斗力为F时,逃离的天数期望。(答案是dp[f])。则有公式。
dp[F]= Σ 1/n * t[i] ,F>c[[i]
+∑ 1/n * dp[F+c[i]] ,F<=c[i]
经验:
数学期望题目大多数需要逆推。 此处逆推的方式是记忆化。而且此题,F是单增的,而且有很明显的边界,即F达到大于最大的C[]的时候,就不会再向下面搜索了,所以记忆化搜索很有效。实在想不出顺推的DP,就记忆化逆推吧,不然DP烧脑壳。
代码:
#include"bits/stdc++.h" #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const ll INF = 0x3fffffffffffffff;
//int t;
db dp[N];
int c[N];
int t[N];
int n,f;
db dfs(int u)
{
if(dp[u]>) return dp[u];
for(int i=;i<n;i++){
if(u>c[i]) dp[u]+=1.0*t[i]/n;
else dp[u]+=(dfs(u+c[i])+)/n;
}
if(u==f) printf("%.3f\n",dp[f]);
return dp[u];
}
int main()
{ while(scanf("%d%d",&n,&f)==){
for(int i=;i<n;i++) ci(c[i]),t[i]=int((+sqrt(5.0))/*c[i]*c[i]);
memset(dp,, sizeof(dp));
dfs(f);
}
return ;
}
ZOJ3640 概率DP的更多相关文章
- Codeforces 28C [概率DP]
/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...
- HDU 4405 Aeroplane chess (概率DP)
题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i 这个位置到达 n ...
- POJ 2096 Collecting Bugs (概率DP)
题意:给定 n 类bug,和 s 个子系统,每天可以找出一个bug,求找出 n 类型的bug,并且 s 个都至少有一个的期望是多少. 析:应该是一个很简单的概率DP,dp[i][j] 表示已经从 j ...
- POJ 2151 Check the difficulty of problems (概率DP)
题意:ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 ,求每队至少解出一题且冠军队至少解出N道题的概率. 析:概率DP,dp[i][j][k] 表示第 i 个队伍,前 j 个题,解出 ...
- 概率DP light oj 1030
t组数据 n块黄金 到这里就捡起来 出发点1 到n结束 点+位置>n 重掷一次 dp[i] 代表到这里的概率 dp[i]=(dp[i-1]+dp[i-2]... )/6 如果满6个的话 否则 ...
- hdu 4050 2011北京赛区网络赛K 概率dp ***
题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...
- [转]概率DP总结 by kuangbin
概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...
- SGU 422 Fast Typing(概率DP)
题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...
- HDU 4050 wolf5x(动态规划-概率DP)
wolf5x Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
随机推荐
- intellijidea课程 intellijidea神器使用技巧 4-2 抽取
1 抽取变量 Ctrl alt V 抽取变量 Ctrl alt C 抽取静态变量 顺带创建静态方法 Crrl alt F 抽取成员变量 2 抽取方法参数 Ctrl alt P 抽取参数,到方法体中 ...
- Django auth组件拓展 关联外部信息---------------------------- Profile 模式
https://docs.djangoproject.com/en/2.1/topics/auth/customizing/ 官方文档. 网上的get_profile 方法不好用太假了 可能我没用明白 ...
- iphone 微信下浏览器中数字去除下划线
在开发iphone应用程序的时候,safari下手机号码默认是有下划线的,通过下面的方法就可以去掉: <meta name="format-detection" conten ...
- mysql自增ID
InnoDB引擎的表,执行清空操作之后,表的auto_increment值不会受到影响:一旦重启MySQL,auto_increment值将变成1. MyISAM引擎的表,执行清空操作之后,表的aut ...
- 软件的依赖关系:类图关系:is-a has-a use-a
基本描述: 类图关系:is-a has-a use-a: 依赖规则: 依赖倒置: 依赖注入: 接口隔离: 无环依赖: 稳定依赖: 依赖倒置描述: 底层抽象-->高层抽象 ^ ^ ...
- Rich feature hierarchies for accurate object detection and semantic segmentation(RCNN)
https://zhuanlan.zhihu.com/p/23006190?refer=xiaoleimlnote http://blog.csdn.net/bea_tree/article/deta ...
- 旧文备份:对象字典0x1005和0x1006的理解
SYNC不一定由主站产生,因此,产生SYNC的节点,0x1005对象的值一般是0x40000080,第30位为1表示本节点产生 SYNC,而本节点的0x1006对象就是产生同步周期值了;而接收SYNC ...
- 第47章 QR-Decoder-OV5640二维码识别—零死角玩转STM32-F429系列
第47章 QR-Decoder-OV5640二维码识别 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.y ...
- idea和eclipse中getAbsolutePath()方法获取值不同
项目中,使用嵌入式tomcat启动web工程(具体使用请度娘,关键字: tomcat embeded) 启动时,设置tomcat path的代码如下: Embedded tomcat = new Em ...
- Spring3声明式事务处理事务无法回滚rollback分析(annotation与xml配置混用)
新项目试运行,DBA提示生产数据库一个表的事务20分钟都未提交,分析过程如下: 1.查看日志log文件,最近20分钟是否有error日志: 2.发现某表有insert错误日志,初步判断由该表插入异常, ...