Robberies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 20073 Accepted Submission(s): 7434
For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.
His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.
Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .
Notes and Constraints
0 < T <= 100
0.0 <= P <= 1.0
0 < N <= 100
0 < Mj <= 100
0.0 <= Pj <= 1.0
A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.
一開始寫的時候理解爲簡單的01揹包,計算概率時只是簡單的相加。狀態方程爲
dp[j]=max(dp[j],dp[j-w[i]]+p[i]);
後始終AC不掉,才發現不是簡單的01揹包,換一種思路,把安全概率線當作揹包容量,才得以AC。狀態方程爲
dp[j]=max(dp[j],dp[j-p[i]]*w[i]);
AC代碼如下
#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
int main()
{
int T,N,i,j,p[105],sum;
float w[105],V,dp[10005];
scanf("%d",&T);
while(T--)
{
sum=0;
scanf("%f %d",&V,&N);
V=1-V;
for(i=1;i<=N;i++)
{
scanf("%d %f",&p[i],&w[i]);
w[i]=1-w[i];
sum+=p[i];
}
memset(dp,0,sizeof(dp));
dp[0]=1;
for(i=1;i<=N;i++)
{
for(j=sum;j>=p[i];j--)
dp[j]=max(dp[j],dp[j-p[i]]*w[i]);
}
for(i=sum;i>=0;i--)
{
if(dp[i]>=V)
break;
}
printf("%d\n",i);
}
return 0;
}