Fourth Xi'an University of Posts acm-icpc school tournament guess Sphere

Title Description

Sixty-one to, in order to celebrate this holiday, many businesses have launched a lot of fun games. Tongtong saw a guessing game balls, there are n identical except for the color of the ball, a golf ball out from the business into the box, known probability of the i-color balls come in boxes of ai . Tongtong this method can use the following to determine the color of the ball in the box: the merchant made speculation: "colors are x y balls or the colors of the balls or ........... a ", businesses will answer your guess is right or wrong until you determine with certainty the box of balls hundred percent, the fewer number of guesses, Tongtong can get better gift. To make Tongtong had a happy six-one or invite you to find out an optimal strategy, presented to the owner as little as possible to guess the color of the ball to determine the expected value of the output to guess the number.
Strategy "optimal" means: the minimum expected number of guesses. When you are 100 per cent sure of the box to determine the color of the type of balls, you do not need to continue to guess. For example, if there are two colors of balls, put the box of balls second color, you can guess "is the first color of the balls." Businesses will tell you the "wrong", so you can guess "box of balls is a second color", and 100 per cent sure, so you can guess the ending without the need for additional time to guess. This inquiry is a way to guess the number of times (no matter which one you have not guessed).
 

 

Entry

A first input row n, there are n represents colored balls. n <= 2 000
a second input row n non-negative decimal a1 ~ an, represents the probability of the i th color balls, and add up to a guaranteed.
 

 

Export

Minimum desired output Reserved 7 decimal places.

 

Sample input

3
0.5000000000 0.2500000000 0.2500000000

Sample Output

1.5000000

 

prompt

Under optimal strategy: Ask first time "is a not a golf ball or a third color in the second color balls," If the answer is "no" then you know that is the first color balls, the end of the inquiry; if the answer is "yes" then asked a second time "is not a second color balls."

Official Solution:

Because the questions and answers at any one time, you can determine which half of the Sphere collection contains the object ball, the other half not contain the target ball. Then further division of balls collection contains the object ball until the ball's collection contains target contains only a ball, it can be a hundred percent sure. This resulted in a decision tree (binary form), binary decision tree root node to each leaf of the road to solutions are interim case, it is clear that the depth inquiry number. There are: expectations = Σ (ask the probability of occurrence of each case number *) = Σ (corresponding depth * leaves the probability of it appears in the box). And we know: the code length expect some kind of coding scheme is the same formula this formula Σ (depth * probability elements appear). Ask the minimum desired number of times i.e. the minimum code length desired. The solution to this problem is the classic method - Huffman

We then need to note that the probability is not zero ball into the priority queue.

 1 #include<cstdio>
 2 #include<queue>
 3 using namespace std;
 4 int main()
 5 {
 6     int n;
 7     while(~scanf("%d",&n))
 8     {
 9         priority_queue<double,vector<double>,greater<double> > q;
10         double x,y,ans=0.0;
11         while(n--)
12         {
13             scanf("%lf",&x);
14             if(x>0.0)
15                 q.push(x);
16         }
17         while(q.size()>1)
18         {
19             x=q.top();
20             q.pop();
21             y=q.top();
22             q.pop();
23             ans+=x+y;
24             q.push(x+y); 
25         }
26         printf(".7lf% \ n " , year);
 27      }
 28      return  0 ;
 29 }
Huffman

 

Guess you like

Origin www.cnblogs.com/LMCC1108/p/11115646.html