Valentine's Day

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 583    Accepted Submission(s): 283
Special Judge

 

Problem Description

Oipotato loves his girlfriend very much. Since Valentine's Day is coming, he decides to buy some presents for her.

There are n presents in the shop, and Oipotato can choose to buy some of them. We know that his girlfriend will possibly feel extremely happy if she receives a present. Therefore, if Oipotato gives k presents to his girlfriend, she has k chances to feel extremely happy. However, Oipotato doesn't want his girlfriend to feel extremely happy too many times for the gifts.

Formally, for each present i, it has a possibility of Pi to make Oipotato's girlfriend feel extremely happy. Please help Oipotato decide what to buy and maximize the possibility that his girlfriend feels extremely happy for exactly one time.

Input

There are multiple test cases. The first line of the input contains an integer T (1≤T≤100), indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤10 000), indicating the number of possible presents.

The second line contains n decimals Pi (0≤Pi≤1) with exactly six digits after the decimal point, indicating the possibility that Oipotato's girlfriend feels extremely happy when receiving present i.

It is guaranteed that the sum of n in all test cases does not exceed 450000.

Output

For each test case output one line, indicating the answer. Your answer will be considered correct if and only if the absolute error of your answer is less than 10−6.

Sample Input

2

3

0.100000 0.200000 0.900000

3

0.100000 0.300000 0.800000

Sample Output

0.900000000000

0.800000000000

 

 

 

题目大意

有n个物品,每个物品有Pi的概率使GF开心,你要从中选出一些物品送给GF,求出GF恰好开心一次的最大概率。

 

 

题解

明显的贪心

把物品从大到小排序,保存一下当前的s0和s1,s0表示都不高兴的概率,s1表示恰好高兴一次的概率

当s1>s0时,继续选后面的物品就不优了,就直接退出

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 10005
double p[N];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        double s1=0,s0=1;
        int n,i;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%lf",&p[i]);
        sort(p+1,p+n+1);
        for(i=n;i>=1;i--){
            s1=s0*p[i]+s1*(1-p[i]);
            s0=s0*(1-p[i]);
            if(s1>s0) break;
        }
        printf("%.12f\n",s1);
    }
}