This question is like this. It gives the hero the probability of success in a pile of events. He just wants to make one successful.
As a result, the question is: what events should he choose to do to maximize the probability of his thoughts.
My first thought was enumeration. I thought it was too troublesome to use DFS for enumeration.
So I figured out if there were any rules, so I deduced it, pushed it out, and wrote it into the code and submitted it and found it was wrong.
In the end, there was no way, and the rest of the time was not enough to write the DFS, so I gave up.
Today, I read thnkndblv's Code. The Code is very short, so I thought it was definitely a mathematical rule. So I looked at it,
Indeed.
Whether this is the case or enumeration, of course it is skillful. Let me explain it.
If n events exist together,
Select one task to do. The maximum probability of implementation is,
Select two things to do, the maximum probability of implementation is, and N things to do.
So we will get n results. The biggest one in it is the maximum probability of realizing the idea of the hero.
In this case, we need to randomly select one thing from N events, then compare the probability, randomly select two things, and then compare the probability ...... Randomly select n tasks and then compare the probability
You still need to search.
However, there is a mathematical knowledge here, so you don't need to search. The practice is as follows:
Sorts the success probability of N events from large to small,
Then,
The probability of selecting the first thing to do successfully is to "select one thing to do from it, the maximum implementation probability"
The probability of selecting the first two things for success is to "select two things for success, the maximum implementation probability"
And so on.
With this, this question is very easy. As for why it can be proved, it cannot be proved. If someone proves it, please leave a message to discuss it, the source question and address and the code of my subsequent AC are attached as follows:
Http://codeforces.com/contest/443/problem/D
D. Andrey and problemtime limit per Test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
Andrey needs one more problem to conducting CT a programming contest. He hasNFriends who are always willing to help. he can ask some of them to come up with a contest problem. andrey knows one value for each of his fiends-the probability that this friend will come up with a problem if Andrey asks him.
Help Andrey choose people to ask. as he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. you need to choose such a set of people that maximizes the chances of Andrey not getting upset.
Input
The first line contains a single integerN(1? ≤?N? ≤? 100)-the number of Andrey's friends. The second line containsNReal NumbersPI(0.0? ≤?PI? ≤? (1.0)-the probability thatI-Th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point.
Output
Print a single real number-the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most10? -? 9.
#include<iostream>#include<algorithm>using namespace std;bool cmp(double a,double b){return a>b;}int main(){int n,i,j,k;double a[110],x,sum,MAX;scanf("%d",&n);for(i=0;i<n;i++)scanf("%lf",&a[i]);sort(a,a+n,cmp);MAX=0;for(i=1;i<=n;i++){sum=0;for(j=0;j<i;j++){x=a[j];for(k=0;k<i;k++){if(j!=k)x*=1-a[k];}sum+=x;}MAX=max(sum,MAX);}printf("%.12lf\n",MAX);}
Codeforces #253 D-Andrey and problem mathematical knowledge