(2019hdu多校第十场1003) Valentine's Day

 

Valentine's Day

时间限制: 1 Sec  内存限制: 1024 MB  Special Judge

题目描述

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.

 

输入

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.

 

输出

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.
 
方法:
签到题,要求只高兴一次切概率最高
显然,按概率从大到小排序后便利第i次使他的女朋友高兴,当总的概率没有i-1高时break;
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e4+15;
double a[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int _,n;
    cin>>_;
    while(_--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        sort(a+1,a+1+n);
        reverse(a+1,a+1+n);
        double ans=a[1], tmp=(1-a[1]);
        for(int i=2;i<=n;i++)
        {
            double now=tmp*a[i]+ans*(1-a[i]);
            if(now>=ans)
            {
                ans=now;
                tmp*=(1-a[i]);
            }
            else break;
        }
        printf("%.9lf\n",ans);
    }
    return 0;
}

 

posted @ 2019-08-22 19:42  Suiyue_Li  阅读(302)  评论(0编辑  收藏  举报