Hang electric field of more than 10 school

c question: hdu6693

Meaning of the questions: given the current probability value of n to buy goods very happy and asked to buy from a 1-n, selected from all purchased a happy maximum probability value it.

A good understanding of the meaning of problems, first of all from what we start with, how are we to think of this title, the first clear point is that you can choose one, two or election, or the election three, four. . . . n th.

Case of large cases can be small in reasoning out

1: First of all, if you pick one directly select the maximum on it?

2: If you select two, may wish to set pi <pj there pi * (1-pj) + pj * (1-pi) This is the only happy once expectations then there is pi + pj-2 * pi * pj to choose two, he certainly will choose can be larger. . . .

I.e., pi + pj-2 * pi * pj> pj have pi -2 * pi * pj> 0 get pi * (1-2 * pj)> 0 to obtain pj <0.5 Similarly selected from 3, 4, we also selected to satisfy my this principle. . . . .

3: how to choose it then select multiple words. Thus selected, then it is clear that it can be seen from the two, provided pi <pk <pj <0.5 there pi * (1-pj) + pj * (1-pi) = pi + pj-2 * pi * pj have pk + pj-2 * pk * pj

There pk + pj-2 * pk * pj -pi -pj + 2 * pi * pj = pk-pi -2pj (pk-pi) = (pk-pi) (1-2pj)> 0 so that the selected Description large as possible. . . . . . . . .

4: Then the algorithm will be apparent to obtain a first sorted, select a maximum, and then find <0.5 = number of positions, start looking forward from this position, is selected from one selected from two, three election until the election when complete unit can be. . . . . . . . . .

5: Then look at this list formula to save what you can, below is the code.

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <bitset>
 6 #include <iostream>
 7 typedef long long ll;
 8 using namespace std;
 9 const int maxn=10200;
10 int t,n;
11 double pi[maxn];
12 int main(){
13     scanf("%d",&t);
14     while(t--){
15         scanf("%d",&n);
16         for(int i=1;i<=n;i++) scanf("%lf",&pi[i]);
17         sort(pi+1,pi+n+1);
18         int tip=0;
19         for(int i=1;i<=n;i++){
20             if(pi[i]<=0.5){
21                 tip=i;
22             }
23         }
24         if(tip==0) printf("%.10lf\n",pi[n]);
25         else{
26             double ans=pi[n];
27             double p1=1.00-pi[tip],p2=pi[tip];
28             for(int i=tip-1;i>=1;i--){
29                 ans=max(ans,pi[i]*p1+(1-pi[i])*p2);
30                 p2=pi[i]*p1+(1-pi[i])*p2;
31                 p1=p1*(1-pi[i]);
32             }
33             printf("%.10lf\n",ans);
34         }
35     }
36     return 0;
37 }
View Code

 

Guess you like

Origin www.cnblogs.com/pandaking/p/11516911.html