首页 > 代码库 > ZOJ3640 Help Me Escape(概率dp)

ZOJ3640 Help Me Escape(概率dp)

p 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

	题目大意:给你一个初始值f,有n个洞口,你每次被随机分到这n个洞口的其中一个,如果你的f>c[i](洞的防御力),那么就可以跳出了,需要的天数是p*c[i]*c[i],如果f<=c[i],那么他的攻击力变为f+c[i],然后又随机到一个洞口,天数加1.求最后出去天数的期望值。
	解题思路:用dp[i]表示战斗力为i时出去的期望。
如果i>c[j]   dp[i]+=(int)(p*c[j]*c[j])/n;
如果i<=c[j]  dp[i]+=(dfs(i+c[j])+1)/n;
	概率的题目要从后往前推。。。。又郁闷了好久。
	然后就可以记忆化dfs.
	题目地址:Help Me Escape
AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=200005;
const int maxm=10005;
const double eps=1-6;

const double p=(1.0+sqrt(5.0))/2.0;
double dp[maxn];
int c[maxm],t[maxm],n;

double dfs(int x)
{
    if(dp[x]>0) return dp[x];

    for(int i=0; i<n; i++)
    {
        if(x>c[i])
            dp[x]+=(double)t[i]/n;
        else
            dp[x]+=(dfs(x+c[i])+1.0)/n;
    }
    return dp[x];
}

int main()
{
    int f;
    int i,j;
    while(cin>>n>>f)
    {
        memset(dp,0,sizeof(dp));
        for(i=0; i<n; i++)
        {
            scanf("%d",&c[i]);
            t[i]=p*c[i]*c[i];
        }

        double ans=dfs(f);
        printf("%.3f\n",ans);
    }
    return 0;
}

/*
3 1
1 2 3
4 1
10 100 1000 10000
*/