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个银行,每个银行有一定的资金和抢劫后被抓的概率,在 ...
随机推荐
- node.js(四)path优化(路径优化)
1.normalize函数的基本用法 normalize函数将不符合规范的路径经过格式化转换为标准路径,解析路径中的.与..外,还能去掉多余的斜杠. 如下示例: var path = require( ...
- 无法加载协定为“XXXWebServiceSoap”的终结点配置部分,因为找到了该协定的多个终结点配置
错误描述:无法加载协定为“XXXWebServiceSoap”的终结点配置部分,因为找到了该协定的多个终结点配置.请按名称指示首选的终结点配置部分. 错误原因:该webservce在web.confi ...
- android入门——数据存储
首先是SharedPreferences 用户偏好 package com.example.wkp.aboutdata; import android.content.Intent; import a ...
- hdu4753
很简单的位模拟(bit-mask),可惜队友读题误以为很难,没有及时跟我交流,不然应该很早就可以出了. 很容易看出来,总共才16个点.24条边.用一个int类型数字就可以描述这个图了,按照16点的关系 ...
- BZOJ 1396: 识别子串( 后缀数组 + 线段树 )
这道题各位大神好像都是用后缀自动机做的?.....蒟蒻就秀秀智商写一写后缀数组解法..... 求出Height数组后, 我们枚举每一位当做子串的开头. 如上图(x, y是height值), Heigh ...
- mysql utf8_bin跟utf8_general_ci的区别
ci是 case insensitive, 即 "大小写不敏感", a 和 A 会在字符判断中会被当做一样的; bin 是二进制, a 和 A 会别区别对待. 例如你运行: SEL ...
- WordPress下载安装简单配置实例
1.下载https://cn.wordpress.org/ 2.复制wp-config-sample.php为wp-config.php 3.创建一个wordpress数据库 4.修改wp-confi ...
- Java判断字符串是否为空的三种方法
方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低.1: if(s == null || s.equals("")); 方法二: 比较字符串长度, 效率高, 是我知道的最 ...
- linux----定义命令别名
1.定义命令别名的语法: alias nickName='command'#用于定义. unalias nickName#用于撤消一个别名的定义. 如:alias cls='clear' 2.应该要 ...
- poj2190
#include <stdio.h> #include <stdlib.h> int main() { ]; ,i; scanf("%s",arr); ;i ...