Andrey and Problem题解
Andrey and Problem题解
我们显然可以转化为:
找出某一种顺序,按此种顺序算概率,使概率最大:
对于一点i,之前答案为:ans,则现在答案为:\(ans*(1-p_{i})+p_{i}*\prod_{j=1}^{i-1}(1-p_{j})=ans+p_{i}*(\prod_{j=1}^{i-1}(1-p_{j})-ans)\)
\(ans\)和\(\prod_{j=1}^{i-1}(1-p_{j})-ans\)都为定值,所以要使答案最大,即让\(p_i\)最大,按\(p_i\)从大到小排序即可。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+6;
const double eps=1e-8;
int n;
double p[N],mul=1.00,ans=0;
bool cmp(double u,double v){return u>v;}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%lf",&p[i]);
sort(p+1,p+n+1,cmp);
for(int i=1;i<=n;++i){
if(mul-ans>eps) ans=ans+p[i]*(mul-ans);
else break;
mul=mul*(1-p[i]);
}
printf("%.10lf",ans);
return 0;
}