Codeforces - Andrey and Problem

题目链接:Andrey and Problem


考虑加入某个数字的贡献:
若当前1个人同一的概率为x,0个人同一的概率为y,当前加入的人概率为p
P = x*(1-p) + p*y
P = x + p * (y - x)
可以看出,如果y>x就可以一直加人进来,加谁呢?最大的p!

AC代码:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=110;
double p[N],res,x=1.0;	int n;
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>p[i];
	sort(p+1,p+1+n);
	for(int i=n;i>=1;i--){
		if(x>res)	res+=(x-res)*p[i];
		else	break;	x*=(1.0-p[i]); 
	}
	printf("%.12lf\n",res);
	return 0;
}
发布了579 篇原创文章 · 获赞 242 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/104271946