ZOJ3640-Help me escape

Source: Internet
Author: User
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 caveNPaths. Due to Lord's punishment, all the paths are zigzag and dangerous. The difficulty ofIthPATH isCi.

Then we defineFAs the fighting capacity of Cain. Every day, Cain will be sent to one ofNPaths randomly.

Suppose Cain is in front ofIthPath. He can successfully takeTiDays to escape from the cave as long as his fighting capacityFIs largerCi. Otherwise, he has to keep trying day after day. However, if Cain failed to escape, his fighting capacity wowould increaseCiAs the result of actual combat. (A kindly reminder: Cain will never died .)

AsTi, We can easily draw a conclusion thatTiIs closely relatedCi. 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 integersNAndF(N≤ 100,F≤ 10000) are given in the first line. The second line between des n positive integersCi(Ci≤ 10000, 1 ≤I≤ N)

Output

For each case, You shoshould output the expectation (3 digits after the decimal point ).

Sample Input
3 11 2 3
Sample output
6.889
 
There are n channels, each of which has a CI value. Now one person needs to pick a path to escape from the path, and each path has the same probability. At the beginning, this person has a f value, he randomly chooses a path. If the F value is greater than CI, it will take ti days for him to escape. If the F value is smaller than CI, he will increase the CI size and repeat the process, ask the mathematical expectation of the days needed to escape
 
Practice: DP [f] indicates the number of days required to escape when the current combat capability of this person is F. Transfer is N...
 
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <string>#include <algorithm>#include <queue>#include <cmath>using namespace std;const int maxn = 100000+10;struct path{    int c,t;    path(int c,int t):c(c),t(t){}};vector<path> vp;int n,f;double dp[maxn];bool vis[maxn];double dfs(int f){    if(vis[f])  return dp[f];    vis[f] = 1;    double ans = 0;    for(int i = 0; i < n; i++){        if(vp[i].c >= f){            ans += (1+dfs(f+vp[i].c))/n;        }else{            ans += double(vp[i].t)/n;        }    }    return dp[f] = ans;}int main(){    while(~scanf("%d%d",&n,&f)){        vp.clear();        memset(vis,0,sizeof vis);        for(int i = 0; i < n; i++){            int t;            scanf("%d",&t);            int k = int((1+sqrt(5.0))/2*t*t);            vp.push_back(path(t,k));        }        printf("%.3lf\n",dfs(f));    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.