Codeforces442B_Andrey and Problem (Greedy)

Source: Internet
Author: User

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 digit ≤ DigitNLimit ≤ limit 100)-the number of Andrey's friends. The second line containsNReal numbersPI(0.0 bytes ≤ averagePILatency ≤ limit 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 most 10 seconds-since 9.

Sample test (s) input
40.1 0.2 0.3 0.8
Output
0.800000000000
Input
20.1 0.2
Output
0.260000000000
Note

In the first sample the best strategy for Andrey is to ask only one of his friends, the most reliable one.

In the second sample the best strategy for Andrey is to ask all of his friends to come up with a problem. then the probability that he will get exactly one problem is 0.1 · 0. 8 cores + cores 0.9 · 0. 2 bytes = average 0.26.

Solution report

If you don't do cf for a long time, you'll need water in the evening... It seems that I am still very tender.

Andrey wants a friend to give a question. He knows the probability of a friend giving a question. He wants only one question and has the highest probability of achieving it.

If Andrey chooses a person to ask, it must have the highest probability. If Andrey chooses two people, it must be the first two people with the highest probability. This is a greedy strategy. If the person with the highest probability is selected, the probability of implementation is itself. If the first two probability P1 and P2 are selected, the probability of implementation is P1 * (1-P2) + (1-P1) * P2.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;double num[1000];int cmp(double a,double b){    return a>b;}int main(){    int n;    while(cin>>n)    {        double sum=0,cnt=0,maxx=0;        for(int i=0; i<n; i++)            cin>>num[i];        sort(num,num+n,cmp);        for(int k=1; k<=n; k++)        {            sum=0;            for(int i=0; i<k; i++)            {                cnt=1;                for(int j=0; j<k; j++)                {                    if(i!=j)                    {                        cnt*=(double)(1-num[j]);                    }                    else cnt*=num[j];                }                sum+=cnt;            }            maxx=max(sum,maxx);        }        printf("%.12lf\n",maxx);    }    return 0;}




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.