Title Description
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 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
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 }