博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Robberies HDU - 2955 ( 0-1背包 )
阅读量:4046 次
发布时间:2019-05-25

本文共 2532 字,大约阅读时间需要 8 分钟。

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

题意:

先给出测试样例的个数t,每个测试样例第一行是总被抓概率P(最后求得的总概率必须小于他,否则就会被抓),然后是想抢的银行数n。然后n行,每行分别是该银行能抢的钱数m和被抓的概率p。求不被抓到的概率内可抢到的最大的金钱数

因为题目给出的是被抓的概率,那么我们就转换为安全概率1-p,然后使用0-1背包。dp[i]就是抢到钱数是i时候的安全概率。最后求出不超过总安全概率(1-P)的最多的i数。

代码:

#include
#include
#include
#include
#include
using namespace std;double dp[105*105];struct node{ int money; //钱数 double p; //安全的概率 }bank[110];int main(){ int t,n; double P; scanf("%d",&t); while(t--) { int m=0; scanf("%lf%d",&P,&n); //被抓的概率,n个银行 P=1.0-P; //安全概率 for(int i=1;i<=n;i++) { scanf("%d%lf",&bank[i].money,&bank[i].p); bank[i].p=1.0-bank[i].p; m+=bank[i].money; //所有银行钱 } memset(dp,0,sizeof(dp)); dp[0]=1; //不抢的时候,安全概率是1 for(int i=1;i<=n;i++) { for(int j=m;j>=bank[i].money;j--) { dp[j]=max(dp[j],dp[j-bank[i].money]*bank[i].p);//dp[j]:抢到j钱的安全概率 } } for(int i=m;i>=0;i--) { if(dp[i]>P) { printf("%d\n",i); break; } } } return 0; }

 

转载地址:http://ytzci.baihongyu.com/

你可能感兴趣的文章
Selenium之前世今生
查看>>
Selenium-WebDriverApi接口详解
查看>>
Selenium-ActionChains Api接口详解
查看>>
Selenium-Switch与SelectApi接口详解
查看>>
Selenium-Css Selector使用方法
查看>>
Linux常用统计命令之wc
查看>>
测试必会之 Linux 三剑客之 sed
查看>>
Socket请求XML客户端程序
查看>>
Java中数字转大写货币(支持到千亿)
查看>>
Java.nio
查看>>
函数模版类模版和偏特化泛化的总结
查看>>
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>