HDU2955-Robberies
描述:
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.
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 .
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.
代码:
首先得转换思路,因为概率值是浮点数,没有办法作为背包的容量。
这里选择抢劫的钱数作为背包容量,dp[i]代表抢劫的钱数为i时,不被抓住的最大的概率(这里比较绕-_-)。只要有一次被抓住,就算被抓住,所以选择计算不被抓住比较方便,只需将1-p[i]累乘即可。当不被抓住的概率最大时,被抓住的概率就最小,使得i值尽可能的大,得到最优解。
初始化时,只有dp[0]=1,其余均为0,因为dp代表确实抢到的数目,而不是最大值,也就是这里要求恰好装满。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define N 105
#define M 10000 int main(){
int T,n,money[N],sum;
double p,dp[M],pos[N];//pos为抢劫一个银行被抓的概率,dp为抢劫该数目的钱一次都不被抓的概率
scanf("%d",&T);
while( T-- ){
scanf("%lf%d",&p,&n);
sum=;
for( int i=;i<=n;i++ ){
scanf("%d%lf",&money[i],&pos[i]);
sum+=money[i];
}
memset(dp,0.0,sizeof(dp));
dp[]=;//只有没有抢到钱,此时有解的概率为1。其余均为0,代表无解
for( int i=;i<=n;i++ ){
for( int j=sum;j>=money[i];j-- ){
dp[j]=max(dp[j],dp[j-money[i]]*(-pos[i]));//j值代表抢钱的数目(并不是最大的数目,而是确实抢到的数目),j值一定,希望dp值越大越好
}
}
for( int i=sum;i>=;i-- ){
if( dp[i]>=-p ){//不被抓的概率大于预期
printf("%d\n",i);
break;
}
}
}
system("pause");
return ;
}
HDU2955-Robberies的更多相关文章
- HDU2955 Robberies[01背包]
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu2955 Robberies(背包)
https://vjudge.net/problem/HDU-2955 概率是浮点数,只能做值(而且这里是累乘,也不能化成整数),这里注意要化成安全概率(1-p[i]),求安全概率的最大值. 钱数作二 ...
- HDU-2955 Robberies 浮点数01背包 自变量和因变量位置互换
题目链接:https://cn.vjudge.net/problem/HDU-2955 题意 突然想找几个银行抢钱. 给出各银行的钱数和被抓的概率,以及能容忍的最大被抓概率. 问他最多能抢到多少钱? ...
- hdu2955 Robberies 01背包+概率
link:http://acm.hdu.edu.cn/showproblem.php?pid=2955 首先,这个题目的背包容量不能是概率.1.精度不清楚.2.把概率相加有什么意义呢?所以,转换一下, ...
- 01标题背包水章 HDU2955——Robberies
原来是dp[i],它代表的不被抓的概率i这最大的钱抢(可能1-100) 客是dp[i]表示抢了i钱最大的不被抓概率,嗯~,弱菜水题都刷不动. 那么状态转移方程就是 dp[i]=max(dp[i],dp ...
- hdu2955 Robberies (01背包)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=2955">http://acm.hdu.edu.cn/showproblem.php ...
- 【题解】 hdu2955 Robberies
有抱负的罗伊·劫匪已经看过很多美国电影,他知道坏人通常会被抓住,经常是因为他们太贪心了.他决定在银行抢劫案中工作一段时间,然后退休后到一所大学从事一份舒适的工作. 题目: 罗伊去几个银行偷盗,他既想多 ...
- Robberies(HDU2955):01背包+概率转换问题(思维转换)
Robberies HDU2955 因为题目涉及求浮点数的计算:则不能从正面使用01背包求解... 为了能够使用01背包!从唯一的整数(抢到的钱下手)... 之后就是概率的问题: 题目只是给出被抓的 ...
- Robberies(简单的01背包 HDU2955)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- 【hdu2955】 Robberies 01背包
标签:01背包 hdu2955 http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意:盗贼抢银行,给出n个银行,每个银行有一定的资金和抢劫后被抓的概率,在 ...
随机推荐
- wikioi-1039-数的划分
将整数n分成k份,且每份不能为空,任意两种划分方案不能相同(不考虑顺序). dp[i][j]:把数i分成k分的方案数 则:dp[i][j]=sum(dp[i-j][t])(t>=1&&a ...
- ACCESS 里面的坑
在vs2005中用sql语句访问access数据库出现replace函数未定义错误,后来查询得知,在不能用replace访问access. 问题: 1.为什么以前运行正常的Access数据库,搬到 ...
- 20141129 LinQ to SQL
ORMO-Object对象R-Relation关系M-Mapping映射 对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是 ...
- C# 与 VB.NET 对比
C# 与 VB.NET 对比 2008-06-20 15:30 by Anders Cui, 1462 阅读, 3 评论, 收藏, 编辑 Table of Contents 1.0 Int ...
- MyEclipse的 lib和Build path(构建路径)(转)
首先两种方式对于放置jar包的方式是不同的: Build path(构建路径):对于种方式来说,可以算是对jar包文件的一个引用.可以引用lib下的jar包,也可以引用本地磁盘上的jar包. WEB- ...
- hdu4753
很简单的位模拟(bit-mask),可惜队友读题误以为很难,没有及时跟我交流,不然应该很早就可以出了. 很容易看出来,总共才16个点.24条边.用一个int类型数字就可以描述这个图了,按照16点的关系 ...
- [poco] HttpRequest之post方法
转自 http://www.cnblogs.com/yuanxiaoping_21cn_com/archive/2012/06/10/2544032.html #import <iostream ...
- javascript 验证身份证
/*身份证号码检索*/ function cardCheck(cartNo) { if (cartNo.val() === "") { return false; } else i ...
- vb listview 的常用操作
常用操作:获取当前行数和列数: MsgBox "行数:" & ListView1.ListItems.Count & "列数:" & L ...
- 数组初始化(c, c++, gcc, g++)
这是很基础的东西,但基础的重要性不言而喻,我敢肯定这个知识点我肯定曾经了解过,但现在,我不敢确定,由此可见纪录的重要性,这世界没有什么捷径,找对方向,然后不停重复.所以从今天开始,我会比较详细的纪录这 ...