Valentine Day HDU 6693

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
Sponsor

一道很神奇的题目,一开始我以为输出最大值就行了

#include<iostream>
#include<cstdio>
#include<algorithm>
#define MAXN 10010
using namespace std;
int T,n;
double p[MAXN];
int main(){
#ifdef WINE
    freopen("data.in","r",stdin);
#endif
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%lf",&p[i]);
        sort(p,p+n);
        double res=p[n-1];
        double no=1; // 已经选的礼物都没有让她开心的概率
        double hap=0; // 之前选的礼物恰好只有一个让她开心的概率
        for(int i=n-1;i>=0;i--){
            if(res>=0.5)break;
            hap=hap*(1-p[i])+no*p[i];
            no*=1-p[i];
            if(hap>res)res=hap;
        }
        printf("%lf\n",res);
    }
    return 0;
}

posted @ 2020-06-22 08:54  winechord  阅读(90)  评论(0编辑  收藏  举报