HDU 6693 [2019 Multi-University Training Contest 10]

Valentine’s Day

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

Solution

oipotato想给女朋友买礼物,每件礼物使女朋友开心的概率是独立的,并且oipotato不想让女朋友开心太多次。(在?为什么你这样都能有女朋友?)让我们求出她女朋友开心的最大概率。
不得不说,HDU的例子一如既往的恶心人(很特殊),一开始以为是直接挑最高概率的就可以,WA了之后,以为是题目读错了,然后认真看了很久的英语,赛后听学长解释也没有读明白,看到标程就秒懂了。
单选最高概率的礼物只存在于有数据大于0.5的情况,这种情况下,礼物越多概率越小。当所有数据都小于0.5的时候,多个礼物叠加,可以让概率更高,只要超过0.5就可以直接退出了。
新的开心概率 = 原先开心概率 × 当前礼物不开心概率 + 原先已选所有礼物都不开心概率 + 当前礼物开心概率

Code

#include
#include
#include
using namespace std;
const int maxn = 1e4+5;
const double eps = 1e-6;

int t,n;
double p[maxn];

int main() {
	scanf("%d",&t);
	while(t--) {
		scanf("%d",&n);
		for(int i=0; i<n; i++) {
			scanf("%lf",&p[i]);
		}
		sort(p,p+n);
		double ans = p[n-1], res = 0, tmp = 1;
		for(int i=n-1; i>=0; i--) {
			if(ans >= 0.5) {
				break;
			}
			res = res * (1 - p[i]) + tmp * p[i];
			tmp = tmp * (1 - p[i]);
			if(res - ans > eps) {
				ans = res;
			}
		}
		printf("%.12lf\n",ans);
	}
}

Source

HDU 6693 Valentine’s Day [2019 Multi-University Training Contest 10]

你可能感兴趣的:(HDU,2019HDU多校赛)