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[205];
double dp[20005],s;
double dfs(int v)
{
    int i;
    if(dp[v]>0)
       return dp[v];
    for(i=1;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)*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,0,sizeof(dp));
        for(i=1;i<=n;i++)
        scanf("%d",&c[i]);
      printf("%.3lf\n",dfs(f));
    }
    return 0;
}

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[205];
double dp[20005],s;
int main()
{
    int i;
    s=(1.0+sqrt(5.0))/2.0;
    while(~scanf("%d%d",&n,&f))
    {
        int max=0;
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++)
          {
              scanf("%d",&c[i]);
              if(max<c[i])
                max=c[i];
          }
               max=2*max;
          dp[max]=0;
          for(i=max;i>=f;i--)
          {
            for(int j=1;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)*1.0/n;
              }

          }
          printf("%.3lf\n",dp[f]);

    }
    return 0;
}

 

posted @ 2014-08-26 16:22  疯狂的癫子  阅读(221)  评论(0编辑  收藏  举报