B. Andrey and Problem

https://codeforces.com/problemset/problem/442/B

题目描述

Andrey needs one more problem to conduct a programming contest. He has nn friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends — the probability that this friend will come up with a problem if Andrey asks him.

Help Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of Andrey not getting upset.

输入格式

The first line contains a single integer nn (1<=n<=100)(1<=n<=100) — the number of Andrey's friends. The second line contains nn real numbers p_{i}pi​ (0.0<=p_{i}<=1.0)(0.0<=pi​<=1.0) — the probability that the ii -th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point.

输出格式

Print a single real number — the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10^{-9}10−9 .

题意翻译

Andrey请教朋友OI题,他知道第ii个朋友解答出问题题的概率p_{i}pi​。

他只想让朋友解答出且仅解答出1道题。

求概率最大。

输入输出样例

输入 #1复制

4
0.1 0.2 0.3 0.8

输出 #1复制

0.800000000000

输入 #2复制

2
0.1 0.2

输出 #2复制

0.260000000000

说明/提示

In the first sample the best strategy for Andrey is to ask only one of his friends, the most reliable one.

In the second sample the best strategy for Andrey is to ask all of his friends to come up with a problem. Then the probability that he will get exactly one problem is 0.1·0.8+0.9·0.2=0.260.1⋅0.8+0.9⋅0.2=0.26 .


看了看其他题解说是能概率dp..感觉最近做到几道dp.然后发现自己dp巨烂..自己觉得这个dp前i个人中选j人有点像背包。..过段时间感觉要专练dp了。

类比背包:每个人都有选或者不选,f[i][j]表示前i个人中选j个人的最大概率,对第i个人选或者不选进行考虑

f[i][j]=max(f[i-1][j],f[i-1][j-1]*(1-p[i])+z[i-1][j-1]*p[i]);

其中z[i][j]表示从前i个人所获得1个题最大概率对应的一个题也不出的概率

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=200;
typedef long long LL;
double f[maxn][maxn],z[maxn][maxn],p[maxn];
int main(void)
{
  //cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  for(LL i=1;i<=n;i++) cin>>p[i];
  for(LL i=0;i<=n;i++) z[0][i]=z[i][0]=1.0; 
  for(LL i=1;i<=n;i++)
  	for(LL j=1;j<=i;j++)
	{
		if(f[i-1][j]<f[i-1][j-1]*(1-p[i])+z[i-1][j-1]*p[i])
		{
			f[i][j]=f[i-1][j-1]*(1-p[i])+z[i-1][j-1]*p[i];
			z[i][j]=z[i-1][j-1]*(1-p[i]);	
		}  	
		else
		{
			f[i][j]=f[i-1][j];
			z[i][j]=z[i-1][j];
		}
		
	} 
	double ans=0;
	for(LL i=1;i<=n;i++) ans=max(ans,f[n][i]);
	printf("%.12f\n",ans);
return 0;
}


思路:贪心+枚举。

假设现在已经有概率x了,看新加一个概率后式子是多少,进行观察。

这道题的启示:适当用连乘连和符号可以帮助数学思考。数学贪心题可以列出当前状态的信息,再随意加入下一个状态,看看合成的式子的状态。

思路参考:da32s1da

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
const double eps=1e-9;
double a[maxn];
bool cmp(double k,double p)
{
	return k>p;
}
int main(void)
{
  //cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  for(LL i=1;i<=n;i++) cin>>a[i];
  sort(a+1,a+1+n,cmp);
  double ans=0;double fac=1.0;double sum=0;
  for(LL i=1;i<=n;i++)
  {
  	ans=ans+a[i]*(fac-ans);
  	sum=max(sum,ans);
  	fac=fac*(1-a[i]);
  }
  printf("%.10f\n",sum);
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/108410703