Zoj 3640 help me escape (Probabilistic DP recursion expectation)

Source: Internet
Author: User

Question Link

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

Question:

A vampire is trapped and has n ways to escape. Each road has a difficulty C []. His initial combat power is F. For the I-th Road, if F> C [I], he will spend t [I] days to go out. Otherwise, he will stay for one day, increase the fighting capacity of C [I], and then select another way to go out, the probability of each route is the same. Ask him about the days he expected to escape.

Note that t [I] is an integer.

Analysis:

D [I] indicates the desire to escape when the combat capability is I.

The advantage of recursion is that the idea is clear, that is, the expectation of when to escape each time according to the probability.

This question also has a reverse recurrence approach.

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]; // note that the array is doubled, because the final state may be 20000.12 int C [maxn], n; 13 14 double DFS (INT f) 15 {16 if (d [f]> 0) return d [f]; // equivalent to pruning, calculated 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 else24 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", & 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}

Zoj 3640 help me escape (Probabilistic DP recursion expectation)

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.