cf442B Andrey and Problem

B. Andrey and Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Andrey needs one more problem to conduct a programming contest. He has n friends 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 integer n (1 ≤ n ≤ 100) — the number of Andrey's friends. The second line contains n real numbers pi(0.0 ≤ pi ≤ 1.0) — the probability that the i-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 - 9.

Sample test(s)
input
4
0.1 0.2 0.3 0.8
output
0.800000000000
input
2
0.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 + 0.9·0.2 = 0.26.


我吓坏了……div1就过了一道B题竟然rating从1700+飞到1800

就是有n个数,第i个数有a[i]的几率出现1,有(1-a[i])的几率出现0。求随意取数使和为1的概率最大

事实上这算法没有严密的证明,姑且算是贪心+dp

首先a数组从大到小排序,由于直觉上感觉a[i]越大越easy凑出1,这样取的数最少,应该概率最大(我说过非常不严密。勿喷)

然后dp

f[i][0]表示前i个数取到和为0的概率,f[i][1]表示前i个数取到和为1的概率

f[i][0]仅仅从f[i-1][0]转移过来。f[i][1]要从f[i-1][0]和f[i-1][1]转移过来

详细看代码。非常短

#include<cstdio>
#include<algorithm>
using namespace std;
int n;
double a[1001],f[1001][2],ans;
bool cmp(double a,double b){return a>b;}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)scanf("%lf",a+i);
    sort(a+1,a+n+1,cmp);
    f[0][0]=1;
    for (int i=1;i<=n;i++)
    {
      f[i][0]=f[i-1][0]*(1-a[i]);
      f[i][1]=f[i-1][0]*a[i]+f[i-1][1]*(1-a[i]);
    }
    for (int i=1;i<=n;i++)
      ans=max(ans,f[i][1]);
    printf("%.12lf",ans);
}


版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/zfyouxi/p/4842672.html