描述:

  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的更多相关文章

  1. HDU2955 Robberies[01背包]

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. hdu2955 Robberies(背包)

    https://vjudge.net/problem/HDU-2955 概率是浮点数,只能做值(而且这里是累乘,也不能化成整数),这里注意要化成安全概率(1-p[i]),求安全概率的最大值. 钱数作二 ...

  3. HDU-2955 Robberies 浮点数01背包 自变量和因变量位置互换

    题目链接:https://cn.vjudge.net/problem/HDU-2955 题意 突然想找几个银行抢钱. 给出各银行的钱数和被抓的概率,以及能容忍的最大被抓概率. 问他最多能抢到多少钱? ...

  4. hdu2955 Robberies  01背包+概率

    link:http://acm.hdu.edu.cn/showproblem.php?pid=2955 首先,这个题目的背包容量不能是概率.1.精度不清楚.2.把概率相加有什么意义呢?所以,转换一下, ...

  5. 01标题背包水章 HDU2955——Robberies

    原来是dp[i],它代表的不被抓的概率i这最大的钱抢(可能1-100) 客是dp[i]表示抢了i钱最大的不被抓概率,嗯~,弱菜水题都刷不动. 那么状态转移方程就是 dp[i]=max(dp[i],dp ...

  6. hdu2955 Robberies (01背包)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=2955">http://acm.hdu.edu.cn/showproblem.php ...

  7. 【题解】 hdu2955 Robberies

    有抱负的罗伊·劫匪已经看过很多美国电影,他知道坏人通常会被抓住,经常是因为他们太贪心了.他决定在银行抢劫案中工作一段时间,然后退休后到一所大学从事一份舒适的工作. 题目: 罗伊去几个银行偷盗,他既想多 ...

  8. Robberies(HDU2955):01背包+概率转换问题(思维转换)

    Robberies  HDU2955 因为题目涉及求浮点数的计算:则不能从正面使用01背包求解... 为了能够使用01背包!从唯一的整数(抢到的钱下手)... 之后就是概率的问题: 题目只是给出被抓的 ...

  9. Robberies(简单的01背包 HDU2955)

    Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  10. 【hdu2955】 Robberies 01背包

    标签:01背包 hdu2955 http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意:盗贼抢银行,给出n个银行,每个银行有一定的资金和抢劫后被抓的概率,在 ...

随机推荐

  1. JavaScript可以这样用

    javascript:Qrlink(<%#Eval("ActivityType")%>,<%#Eval("ID")%>,<%#Ev ...

  2. Thinkphp利用微信多客服消息推送取货二维码消息

    首先看微信官方的说法: 当用户主动发消息给公众号的时候(包括发送信息.点击自定义菜单.订阅事件.扫描二维码事件.支付成功事件.用户维权), 微信将会把消息数据推送给开发者,开发者在一段时间内(目前修改 ...

  3. js对表单设置了readonly和disabled后的区别

    Readonly和Disabled是用在表单中的两个属性,它们都能够做到使用户不能够更改表单域中的内容.但是它们之间有着微小的差别,总结如下: 1)Readonly只针对input(text / pa ...

  4. 灵光一闪-VS设计界面能访问到private修饰的各种控件

    大家都知道,用VS设计界面时,VS默认控件的访问修饰符为private,但是我就很奇怪,private修饰的字段不是只有类内部才能访问吗? 好神奇的VS,这到底是怎么实现的?难道就是类似文本编辑器的作 ...

  5. C语言栈的实现

    栈是常用的数据结构之一,下面给出一个链式栈的实现~~头文件Stack.h #ifndef Stack_H #define Stack_H typedef int Item; typedef struc ...

  6. Swift 断言

    assert(条件,"输出信息"); 如: let age=-1; assert(age>=0,"age要大于0");

  7. javscript上传图片前预览的方法setPreViewImage()

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Html内容超出标记宽度后自动隐藏

    我们在显示长文本时,往往需要去在C#端去截取字符,但这绝对不是一个好方面,因为我们的长文本往往都是代HTML标记的,你一个载不好,就会出现乱码问题(出现半个HTML标记),而比较好的作法就是通过CSS ...

  9. [原创]obj-c编程17:键值观察(KVO)

    原文链接:[原创]obj-c编程17:键值观察(KVO) 系列专栏链接:objective-c 编程系列 说完了前面一篇KVC,不能不说说它的应用KVO(Key-Value Observing)喽.K ...

  10. J2SE知识点摘记(十七)

    1.        Applet Applet的生命周期分为四个阶段,各阶段分别由init,start,stop和destroy四种方法来具体体现. public void init() 此方法通知A ...