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 的时候的逃出去的期望。

用递归的好处是思路比较清楚,就是每次按照概率加 什么时候能逃出去的期望。

这个题也有逆推 递推的做法。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <queue>
 6 #include <cmath>
 7 #include <algorithm>
 8 #define LL __int64
 9 const int maxn = 100+10;
10 using namespace std;
11 double d[20000+10], t[maxn];  //注意数组开到了2倍,因为终态可能是20000.
12 int c[maxn], n;
13
14 double dfs(int f)
15 {
16     if(d[f]>0) return d[f]; //相当于剪枝,已经计算过的不再计算
17
18     for(int i = 0; i < n; i++)
19     {
20         int tmp = (int)t[i];
21         if(f>c[i])
22             d[f] += 1.0/n*(double)tmp;
23         else
24             d[f] += 1.0/n*(1.0+dfs(f+c[i]));
25     }
26     return d[f];
27 }
28
29 int main()
30 {
31     int f, i;
32     while(~scanf("%d%d", &n, &f))
33     {
34         for(i = 0; i < n; i++)
35         {
36             scanf("%d", &c[i]);
37             t[i] = (double)c[i]*c[i]*1.0*(1.0+sqrt(5.0))/2.0;
38         }
39         memset(d, 0, sizeof(d));
40         printf("%.3lf\n", dfs(f));
41     }
42     return 0;
43 }
时间: 2024-10-15 22:22:12

zoj 3640 Help Me Escape (概率dp 递归求期望)的相关文章

ZOJ 3640 Help Me Escape 概率dp

有一个吸血鬼被困了,有n条路可以逃出去,每条路有一个难度c[],他初始的战斗力是f,对于第i条路,若f > c[i]他花t[i]天就能出去,否则,他就停留一天,同时战斗力增加c[i]然后再选一条路走出去,他走每条路的概率是相同的.问他逃出去的天数的期望. 设dp[i]表示在战斗力为i时逃出去的期望值,那么可推出状态方程 dp[i] = 1/n * t[j](c[j] > i),dp[i] = 1/n * (1+dp[ i+c[j] ] )( c[j] <= i). 需要注意的是终态的确定

ZOJ 3329 One Person Game 【概率DP,求期望】

题意:有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和. 当分数大于n时结束.求游戏的期望步数.初始分数为0 设dp[i]表示达到i分时到达目标状态(即i = n)的期望,pk为投掷k分的概率, p0为回到0的概率则dp[i] = ∑(pk * dp[i + k]) + dp[0] * p0 + 1 ; 都和dp[0]有关系,而且dp[0]就是我们所求,为常数设 dp[i] = A[i] * dp[0] + B[i]; 即为d

[ACM] ZOJ 3329 One Person Game (概率DP,有环,巧妙转化)

One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the

[2013山东ACM省赛] The number of steps (概率DP,数学期望)

The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms -). Now she st

概率dp——逆推期望+循环迭代zoj3329

首先要推出dp[i]的期望方程,会发现每一项都和dp[0]相关, 那我们将dp[i]设为和dp[0]有关的式子dp[i]=a[i]*dp[0]+b[i],然后再回代到原来的期望方程里 然后进行整理,可以发现两个系数a[i],b[i]是可以逆推的,并且通过求出a[0],b[0]可以求出dp[0] #include<bits/stdc++.h> using namespace std; #define maxn 1050 double A[maxn],B[maxn],p[maxn]; int ma

ZOJ 3640 Help Me Escape(概率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 w

【期望DP】 ZOJ 3640 Help Me Escape

通道 题意:吸血鬼在一个洞穴遍地的地方,他拥有初始战斗力,如果战斗力大于了洞穴的c值他就能花时间逃出去,否则他的战斗力增加c,然后随机选择下一个要去的洞穴,问他出去所花时间的期望 思路:设dp[v] ,表示当能力值为v的时的期望.所以方程很容易写了,dp[v] = sum{ ti/n }(v直接逃出去) + sum { (1 + dp[v + c[i]])/n }(下一次逃出去) ;对于路i,如果v大于路i的限制,那么就能够用ti逃出去,概率为{1/n}否则只能进入下一天的尝试,所以需要用的时间

zoj 3640 Help Me Escape(期望)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4808 被这道题坑惨了. 有一个吸血鬼被困了,有n条路可以逃出去,每条路有一个难度c[],他初始的战斗力是f,对于第i条路,若f > c[i]他花t[i]天就能出去,否则,他就停留一天,同时战斗力增加c[i]然后再选一条路走出去,他走每条路的概率是相同的.问他逃出去的天数的期望. 设dp[i]表示在战斗力为i时逃出去的期望值,那么可推出状态方程 dp[i] = 1/n * t[

zoj 3640 Help Me Escape

Help Me Escapehttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4808 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