Valentine's Day

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0
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的概率让女朋友非常开心,现在问买那些商品才能让女朋友恰好非常开心一次的概率最大

思路:

记p为恰好让女朋友非常开心一次的概率,p=Σ(i=1->m)pi*π(j=1->m且i!=j)(1-pj),算了一些样例后发现如果pi>0.5,那么无论再买那些商品都会让总概率变小,
如果pi==0.5,则无论再买那些商品都会让总概率=0.5,所以如果存在pi>=0.5,则我们就取一个最大的大于等于0.5概率的商品,否则商品概率都小于0.5时,
队友通过打表发现将商品排序后,从最大的那个开始连续取,总概率会先递增再递减,且只有一个极大值,现在的问题是最坏的情况是要遍历所有商品,复杂度为O(n)
每次都要重新计算总概率,计算总概率的复杂度为O(n*n),所以这样的总复杂度为O(n*n*n),而题目中n最大为1e4,显然n的立方的复杂度是不能接受的,
遍历的O(n)没什么办法优化,但我们可以把计算总概率的O(n*n)的复杂度降为O(n),这样O(n*n)的复杂度是能支持1e4的数据的,所以现在的问题就是怎么优化计算
原始公式为p=Σ(i=1->m)pi*π(j=1->m且i!=j)(1-pj),可以将其转换为,p=Σ(i=1->m)pi*π(j=1->m)(1-pj)/(1-pi),因为π(j=1->m)(1-pj)与i无关,所以可以把他从累加中提出来,
转换为p=[Σ(i=1->m)pi/(1-pi)]*[π(j=1->m)(1-pj)],于是可以O(n)地计算Σ(i=1->m)pi/(1-pi)和π(j=1->m)(1-pj),最后将两者相乘即为总概率

[概率] HDU 2019 Multi-University Training Contest 10 - Valentine's Day-LMLPHP

 #include<bits/stdc++.h>
using namespace std;
const int amn=1e4+;
struct node{
double p,q;
}nm[amn];
double p[amn],q[amn],ans,cnt,maxn,aa,s,t;
bool cmp(node a,node b){
if(a.p==b.p)return a.q<b.q;
return a.p>b.p;
}
int main(){
int n,T,tp;
ios::sync_with_stdio();
scanf("%d",&T);
while(T--){
scanf("%d",&n);
tp=maxn=;
aa=;
for(int i=;i<=n;i++){
scanf("%lf",&nm[i].p);
maxn=max(maxn,nm[i].p);
nm[i].q=-nm[i].p;
}
if(maxn>=0.5){
ans=maxn;
}
else{
tp=maxn=;
sort(nm+,nm++n,cmp);
ans=nm[].p;
for(int i=;i<=n;i++){
s=,t=;
for(int j=;j<=i;j++)s+=nm[j].p/nm[j].q;
for(int j=;j<=i;j++)t*=nm[j].q;
if(s*t>ans)
ans=s*t;
else break;
}
}
printf("%.12lf\n",ans);
}
}
/**
有n种商品,每种商品有pi的概率让女朋友非常开心,现在问买那些商品才能让女朋友恰好非常开心一次的概率最大
记p为恰好让女朋友非常开心一次的概率,p=Σ(i=1->m)pi*π(j=1->m且i!=j)(1-pj),算了一些样例后发现如果pi>0.5,那么无论再买那些商品都会让总概率变小,
如果pi==0.5,则无论再买那些商品都会让总概率=0.5,所以如果存在pi>=0.5,则我们就取一个最大的大于等于0.5概率的商品,否则商品概率都小于0.5时,
队友通过打表发现将商品排序后,从最大的那个开始连续取,总概率会先递增再递减,且只有一个极大值,现在的问题是最坏的情况是要遍历所有商品,复杂度为O(n)
每次都要重新计算总概率,计算总概率的复杂度为O(n*n),所以这样的总复杂度为O(n*n*n),而题目中n最大为1e4,显然n的立方的复杂度是不能接受的,
遍历的O(n)没什么办法优化,但我们可以把计算总概率的O(n*n)的复杂度降为O(n),这样O(n*n)的复杂度是能支持1e4的数据的,所以现在的问题就是怎么优化计算
原始公式为p=Σ(i=1->m)pi*π(j=1->m且i!=j)(1-pj),可以将其转换为,p=Σ(i=1->m)pi*π(j=1->m)(1-pj)/(1-pi),因为π(j=1->m)(1-pj)与i无关,所以可以把他从累加中提出来,
转换为p=[Σ(i=1->m)pi/(1-pi)]*[π(j=1->m)(1-pj)],于是可以O(n)地计算Σ(i=1->m)pi/(1-pi)和π(j=1->m)(1-pj),最后将两者相乘即为总概率
**/
05-27 03:45