ZOJ- 3640 Help Me Escape
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[i],那么只需要花费ti(ti = (1.0 + sqrt(5.0))/2 * c[i] * c[i]) 天的时间就可以逃出去,否则,花费1天的时间,吸血鬼的能力增加c[i],花费1天的时间,然后继续下一天的尝试。求逃出去的期望。
设dp[v] ,表示当能力值为v的时的期望。所以方程很容易写了,dp[v] = sum{ ti/n }(v直接逃出去) + sum { (1 + dp[v + c[i]])/n }(下一次逃出去) ;对于路i,如果v大于路i的限制,那么就能够用ti逃出去,概率为{1/n}否则只能进入下一天的尝试,所以需要用的时间为dp[v + c[i]] + 1 ,概率为{1/n}; 直接使用记忆化搜索的方式写。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,f,c[];
double dp[],s;
double dfs(int v)
{
int i;
if(dp[v]>)
return dp[v];
for(i=;i<=n;i++)
{
if(v>c[i])
dp[v]=dp[v]+int(s*c[i]*c[i])*1.0/n;
else
dp[v]=dp[v]+(dfs(v+c[i])+)*1.0/n;
}
return dp[v];
}
int main()
{
int i;
s=(1.0+sqrt(5.0))/2.0;
while(~scanf("%d%d",&n,&f))
{
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
scanf("%d",&c[i]);
printf("%.3lf\n",dfs(f));
}
return ;
}
dp【f】 表示攻击值为f期望出去的天数:则最大的max,2*max一定是0,dp[2*max]一定是0.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,f,c[];
double dp[],s;
int main()
{
int i;
s=(1.0+sqrt(5.0))/2.0;
while(~scanf("%d%d",&n,&f))
{
int max=;
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
{
scanf("%d",&c[i]);
if(max<c[i])
max=c[i];
}
max=*max;
dp[max]=;
for(i=max;i>=f;i--)
{
for(int j=;j<=n;j++)
{
if(i>c[j])
dp[i]+=int(s*c[j]*c[j])*1.0/n;
else
dp[i]+=(dp[i+c[j]]+)*1.0/n;
} }
printf("%.3lf\n",dp[f]); }
return ;
}
ZOJ- 3640 Help Me Escape的更多相关文章
- ZOJ 3640 Help Me Escape:期望dp
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3640 题意: 有一个吸血鬼被困住了,他要逃跑... 他面前有n条 ...
- zoj 3640 Help Me Escape (概率dp 递归求期望)
题目链接 Help Me Escape Time Limit: 2 Seconds Memory Limit: 32768 KB Background If thou doest w ...
- zoj 3640 Help Me Escape 概率DP
记忆化搜索+概率DP 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...
- Help Me Escape (ZOJ 3640)
J - Help Me Escape Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB ...
- 概率dp ZOJ 3640
Help Me Escape Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- Help Me Escape ZOJ - 3640
Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth ...
- 【ZOJ】3640 Help Me Escape
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4808 题意:一个吸血鬼初始攻击力为f.n条路,他每次等概率选择一条路.如果攻击 ...
- ZOJ Problem Set - 3640 Help Me Escape
题目大意: 有n条路,选每条路的概率相等,初始能力值为f,每条路通过的难度值为ci,当能力值大于某条路A的难度值b时,能够成功逃离,花费时间ti,小于等于时,不能逃离,天数加一天,但能力值增加b. 给 ...
- zoj 3640 概率dp
题意:一只吸血鬼,有n条路给他走,每次他随机走一条路,每条路有个限制,如果当时这个吸血鬼的攻击力大于等于某个值,那么就会花费t天逃出去,否则,花费1天的时间,并且攻击力增加,问他逃出去的期望 用记忆化 ...
- ZOJ 3640
很简单的概率题了 设dp[x]为能力值 为x时出去的期望 天数 #include <iostream> #include <cstdio> #include <cmath ...
随机推荐
- OPENQUERY
SELECT * FROM OPENQUERY(saql007,' SELECT col1,col2,col3 FROM dbname.shemaname.tablename WHERE (1=1 ...
- sql - 查询所有表中包含指定值
可以直接创建sql语句: CREATE TABLE qResults (tName nvarchar(370), cname nvarchar(3630),[count] int) declare @ ...
- C语言malloc()函数:动态分配内存空间
头文件:#include <stdlib.h> malloc() 函数用来动态地分配内存空间(如果你不了解动态内存分配,请查看:C语言动态内存分配及变量存储类别),其原型为:void* m ...
- Hibernate session flush
最近做项目时,用到了hibernnate,批量删除10000条数据时,删除时前台将id传到后台,用in匹配去删除,页面直接卡死. 解决方法,将传过来的10000条id分批删除,每删除五百条后,调用ge ...
- 输出不大于N的素数的个数
输出不大于N的素数的个数 Sieve of Eratosthenes 方法 素数的性质: 非素数可以分解为素数乘积. 证明 (1)n = 2 成立,n = 3 成立: (2)若 n = k 时成立, ...
- win7下装ubuntu
需要的东西有: 1,ubuntu系统镜像,下载地址:http://www.ubuntu.com/download/desktop 选64位吧,兼容性好些. 2,空闲的大于20G硬盘空间,这个大小根据个 ...
- TCP与UDP网络编程总结(一)
(1):TCP网络编程 我们注意到服务端与客户端通信时是通过客户端的套接字相互通信的,那么服务端的套接字主要是干什么用的呢? TCP服务端设置监听套接字时 int listen(int sock,in ...
- 无线端web开发学习总结
无线web开发之前要做一些准备工作:一.必需的reset样式库1.其中的重点是盒模型box-sizing:由原来pc端的content-box改为border-box. *, *:before, *: ...
- cmd 窗口的复制粘贴
如下几种方法1.点击鼠标右键,选择标志,再点击左键拖动选择要复制的内容,然后回车即可复制被 选择的内容 2.点击鼠标右键,选择标志,再点击左键拖动选择要复制的内容,然后点击鼠标右键, 此时就把选择的内 ...
- sharepoint给文档库每个数据条添加权限
前言 老大任务,做一个读取文档库把里面的每一条数据添加权限.挺起来很简单,但是做起来,还是很简单,哈哈.因为我没有接触过这些代码,所以得不断的请教了.大题明白了,简单实现了一下,应用控制台先做了一下简 ...