Robberies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 25589 Accepted Submission(s): 9432
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.
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
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 50005;
double dp[maxn];
struct zeroonebag //定义01背包
{
int v;
double p;
}bag[maxn];
int main()
{
int t,n;
double p;
scanf("%d",&t);
while(t--)
{ //需要低于的概率p,n是银行数,
scanf("%lf%d",&p,&n);
int sum = 0;
for(int i=0; i<n; i++)
{ //bag[i].v 是银行钱数 bag[i].p 抢劫概率
scanf("%d%lf",&bag[i].v,&bag[i].p);
sum = sum+bag[i].v;
}
memset(dp,0,sizeof(dp));
dp[0] = 1; //如果不抢就不会被抓
//外层循环是银行数目,内层循环是银行钱数
for(int i=0; i<n; i++)
{
for(int j=sum; j>=bag[i].v; j--)
{
dp[j] = max(dp[j],dp[j-bag[i].v]*(1-bag[i].p));
}
}
//抢钱的数目和逃跑的概率成反比,抢的越少,越容易跑
for(int i=sum; i>=0; i--)
{
if(dp[i] > 1-p) //最大的逃跑概率大于被抓概率 ,这样才不会被抓
{
printf("%d\n",i);
break;
}
}
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 50005;
double dp[maxn];
int v[maxn];
double w[maxn];
int sum,n;
void zerobag(int cost,double weight)
{
for(int i=sum; i>=cost; i--)
dp[i] = max(dp[i],dp[i-cost]*(1-weight));
}
int main()
{
int t;
double p;
scanf("%d",&t);
while(t--){
scanf("%lf%d",&p,&n);
sum=0;
for(int i=0; i<n; i++)
{
scanf("%d%lf",&v[i],&w[i]);
sum += v[i];
}
memset(dp,0,sizeof(dp));
dp[0]=1;
for(int i=0; i<n; i++)
zerobag(v[i],w[i]);
for(int i=sum; i>=0; i--)
if(dp[i]>=1-p)
{
printf("%d\n",i);
break;
}
}
return 0;
}
1万+




被折叠的 条评论
为什么被折叠?



