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 ...
随机推荐
- JSON语法格式
一.JSON数据格式 名称/值对 二.JSON值对数据类型 数字 字符串 逻辑值 数组(在方括号中) 对象 (在花括号中) null eg: { "staff ...
- 浅谈python的深浅拷贝
python中有两种数据类型:一种是可变数据类型,一种是不可变数据类型 不可变数据类型包括(整型及其他数据类型,字符串及元组) 可变数据类型(列表,集合,字典,类和类实例) 鉴定是否为拷贝还是只是引用 ...
- react-native与原生界面相互跳转
一.添加MyIntentModule类,并继承ReactContextBaseJavaModule实现其方法和构造函数.在该类中添加方法,注意:方法头要加@ReactMethod public cla ...
- nginx-1.12.2编译安装指导
nginx-1.12.2编译安装 下载源码包 安装 安装后配置 下载源码包 下载地址:http://nginx.org/en/download.html nginx-1.12.2:http://ngi ...
- Linq to Sql 左连接 , 取右表可能为 null的 int类型字段
linq to sql , linq to entity 遇到一个问题, 主表, 从表 一对一 关系, 主表有记录, 从表 可能没有记录. 现在要查询 主表+从表 的某几个字段. 从表字段 有的是 ...
- Mahara-16.10 (Ubuntu 16.04)
平台: Ubuntu 类型: 虚拟机镜像 软件包: mahara-16.10 commercial education elearning mahara open source 服务优惠价: 按服务商 ...
- Odoo (OpenERP/TinyERP)-10.0 (Debian 8)
平台: Ubuntu 类型: 虚拟机镜像 软件包: odoo-10.0 commercial erp odoo open source openerp tinyerp 服务优惠价: 按服务商许可协议 ...
- Spark资源管理
Spark资源管理 1.介绍 Spark资源管控分为spark集群自身可支配资源配置和job所用资源配置. 2.spark集群支配资源控制 在spark的conf/spark-env.sh文件中可以指 ...
- BSP和JSP里的UI元素ID生成逻辑
CRM WebClient UI WebClient UI渲染出来的DOM元素的这些C#_W#格式的id是在哪行ABAP代码被生成出来的? 参考我的博客WebClient UI element ID ...
- eclipse快捷键(个人经常使用的)
周末闲来无聊,就把一直以来eclipse使用的快捷键梳理了下,希望对有需要的朋友有所帮助,绝对原创! 1.快速复制一行或者多行 首先选中你要复制的行,如果是一行,则鼠标焦点在那一行即可,如果是多行,可 ...