CodeForces 442B

来源:互联网 发布:2017淘宝手机开店认证 编辑:程序博客网 时间:2024/06/10 15:57

CodeForces 442B

Description

Andrey needs onemore problem to conduct a programming contest. He has n friendswho are always willing to help. He can ask some of them to come up with acontest problem. Andrey knows one value for each of his fiends — theprobability that this friend will come up with a problem if Andrey asks him.

Help Andreychoose people to ask. As he needs only one problem, Andrey is going to bereally upset if no one comes up with a problem or if he gets more than oneproblem from his friends. You need to choose such a set of people thatmaximizes the chances of Andrey not getting upset.

Input

The first linecontains a single integer n (1 ≤ n ≤ 100) — thenumber of Andrey's friends. The second line contains n realnumbers pi(0.0 ≤ pi ≤ 1.0) — theprobability that the i-th friend can come up with a problem. The probabilitiesare given with at most 6 digits after decimal point.

Output

Print a singlereal number — the probability that Andrey won't get upset at the optimal choiceof friends. The answer will be considered valid if it differs from the correctone by at most 10 - 9.

Sample Input

Input

4
0.1 0.2 0.3 0.8

Output

0.800000000000

Input

2
0.1 0.2

Output

0.260000000000

Hint

In the firstsample the best strategy for Andrey is to ask only one of his friends, the mostreliable one.

In the secondsample the best strategy for Andrey is to ask all of his friends to come upwith a problem. Then the probability that he will get exactly one problemis 0.1·0.8 + 0.9·0.2 = 0.26.

一.  题意分析
Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的。
二.  思路过程
用贪心的思路,从概率最大的人开始询问,如果询问他是概率变大,则询问他。
三.  代码

<span style="font-family:Courier New;font-size:18px;">#include <cstdio>#include <cstring>#include <algorithm> using namespace std;const int N = 105;const double eps = 1e-9; int n, c, rec[N];double s, p[N]; double solve (int x) {    double ans = s * p[x];    double tmp = s * (1-p[x]);     for (int i = 0; i < c; i++)        ans += tmp / (1-p[rec[i]]) * p[rec[i]];    return ans;} int main () {    scanf("%d", &n);    for (int i = 0; i < n; i++)        scanf("%lf", &p[i]);     sort (p, p + n);     c = 0;    double ans = p[n-1];    s = 1 - p[n-1];    rec[c++] = n-1;     for (int i = n-2; i >= 0; i--) {        double tmp = solve(i);         if (tmp > ans) {            ans = tmp;            rec[c++] = i;            s *= (1 - p[i]);        }    }     printf("%.12lf\n", ans);    return 0;}</span>



0 0