3 0.04 3 1 0.02 2 0.03 3 0.05 0.06 3 2 0.03 2 0.03 3 0.05 0.10 3 1 0.03 2 0.02 3 0.05
2 4 6
思路: 01 背包问题 dp[i]表示偷价值为 i 时不被抓的概率 表达式为dp[j] = max(dp[j] , dp[j-m[i]] * (1-p[i]));求出不被逮捕的所能强盗的最大钱数。
#include <stdio.h>
#include <string.h>
int m[101];
double p[101],dp[10001];
int main()
{
int t,n,i,j,sum;
double P;
scanf("%d",&t);
while(t--)
{
scanf("%lf %d",&P,&n);
sum = 0;
P = 1 - P;
for(i = 0; i < n; i++)
{
scanf("%d %lf",&m[i],&p[i]);
sum += m[i];
p[i] = 1 - p[i];
}
memset(dp,0,sizeof(dp));
dp[0] = 1;
for(i = 0; i < n; i++)
for(j = sum; j >= m[i]; j--)
if(dp[j] < dp[j-m[i]]*p[i])
dp[j] = dp[j-m[i]]*p[i];
for(i = sum; i >= 0&&dp[i] < P; i--);
printf("%d\n",i);
}
return 0;
}