您的位置:首页 > 其它

HDU 2955-Robberies

2018-02-07 14:21 190 查看


Robberies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 27306    Accepted Submission(s): 10063


题目链接:点击打开链接

Problem Description

The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before
retiring to a comfortable job at a university.



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.

 

Input

The first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line
j gives an integer Mj and a floating point number Pj . 

Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .

 

Output

For each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set.

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.

 

Sample Input

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

 

Sample Output

2
4
6

题意:

有个人想去银行偷钱,现在给出他最大的失败概率,以及银行数量,对于每个银行,也会给出抢劫该银行最大的抢劫概率和本银行所拥有的金钱数量,让求在失败概率尽可能小的情况下,能得到的尽可能大的金钱数量。

分析:

题中说各个银行都是独立的,本题可以将概率转化为成功的概率来写,给出了最大的失败概率,那么我们就可以知道最小的成功概率,然后再进行01背包,因为如果没有抢到钱,那么成功的概率是1,dp数组存的是概率,dp[i]表示抢钱数量为 i 时的成功概率是多少,所以初始化dp时,将dp[0]置为1,其余的都置为0.

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
using namespace std;

int main()
{
int n,v[105],sum;
double u[105],p,dp[10005];
int t;
scanf("%d",&t);
while(t--)
{
sum=0;
memset(dp,0,sizeof(dp));
dp[0]=1;///投的钱为0时的成功概率是1
scanf("%lf %d",&p,&n);
p=1.0-p;///将最大失败率转化为最小成功的概率
for(int i=0;i<n;i++)
{
scanf("%d %lf",&v[i],&u[i]);
u[i]=1.0-u[i];///将失败率都转化为成功的概率
sum+=v[i];///统计所给银行总共的钱数
}
for(int i=0;i<n;i++)///进行01背包
{
for(int j=sum;j>=v[i];j--)
dp[j]=max(dp[j],dp[j-v[i]]*u[i]);
}
for(int i=sum;i>=0;i--)///找到一个符合条件的并且可以抢到的钱数最多的方案
{
if(dp[i]>p)///找到第一个比最小成功率大的概率
{
printf("%d\n",i);
break;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: