盗金库,dp[i]表示抢劫i元逃跑的概率,则dp[j] = max(dp[j], dp[j - v[i]] * (1 - w[i]));
抢劫多个银行逃跑的概率等于抢劫每个银行逃跑概率之积。
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
int v[110];
double w[110],dp[11000];
int main()
{
int i,j,t,n;
double p;
scanf("%d",&t);
while(t--)
{
int sum=0;
scanf("%lf%d",&p,&n);
memset(v,0,sizeof(v));
memset(w,0,sizeof(w));
memset(dp,0,sizeof(dp));
for (i=0; i<n; i++)
{
scanf("%d %lf",&v[i],&w[i]);
sum+=v[i];
}
dp[0]=1;//!
for (i=0; i<n; i++)
{
for (j=sum; j>=v[i]; j--)
{
dp[j]=max(dp[j],dp[j-v[i]]*(1-w[i]));
}
}
for (i=sum; i>=0; i--)
{
if (dp[i]>=(1-p))
{
printf("%d\n",i);
break;
}
}
}
return 0;
}
原文:https://www.cnblogs.com/shidianshixuan/p/13729837.html