题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955
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
题意:Roy想要抢劫银行,每家银行多有一定的金额和被抓到的概率,知道Roy被抓的最大概率P,求Roy在被抓的情况下,抢劫最多。
分析:被抓概率可以转换成安全概率,Roy的安全概率大于1-P时都是安全的。抢劫的金额为0时,肯定是安全的,所以d[0]=1;其他金额初始为最危险的所以概率全为0;注意精度。
转移方程为:dp[j]=max(dp[j-v[i]]*p[i],dp[j]);
如下代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#define esp 1e-10
#define M 10005
using namespace std;int main(){int t;scanf("%d",&t);while(t--){double p[M],sp;//p为抢每个银行的安全概率int v[M];//memset(dp,0,sizeof(dp));double dp[M]={1.0};   //dp为拿完钱后自己的安全概率 初始状态最安全,所以为1.0 int n,s=0;scanf("%lf%d",&sp, &n);sp=1-sp;    //转换成安全概率 for(int i=0; i<n; ++i){scanf("%d%lf", &v[i],&p[i]);s+=v[i];p[i]=1-p[i];    //转换成安全概率 }for(int i=0; i<n; ++i){for(int j=s; j>=v[i]; --j){dp[j]=max(dp[j-v[i]]*p[i],dp[j]);}}for(int i=s; i>=0; --i)if(dp[i]-sp>esp){printf("%d\n",i);break;}}return 0;
}

HD 2955 Robberies(0-1背包)相关推荐

  1. HDOJ 2955 Robberies (01背包)

    10397780 2014-03-26 00:13:51 Accepted 2955 46MS 480K 676 B C++ 泽泽 http://acm.hdu.edu.cn/showproblem. ...

  2. HDUOJ 2955 Robberies

    HDUOJ 2955 Robberies 题目链接 Problem Description The aspiring Roy the Robber has seen a lot of American ...

  3. Python 0/1背包、动态规划

    参考:http://www.cnblogs.com/fcyworld/p/6243012.html Python 0/1背包.动态规划 0/1背包问题:在能承受一定重量的背包中,放入重量不同,价值不同 ...

  4. P1417 烹调方案 (0/1背包+贪心)

    题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...

  5. 算法分析与设计——蛮力法0/1背包

    蛮力法0/1背包 蛮力法 蛮力法是一种简单直接解决问题的方法,常常直接基于问题的描述,所以蛮力法也是最容易应用的方法. 蛮力法所依赖 的基本技术是遍历,即采用一定的策略依次处理待求解问题的所有元素,从 ...

  6. 数据结构与算法 / 回溯算法(八皇后、0 - 1 背包)

    回溯算法,顾名思义,就是在没有得到最优解的前提下,不断的返回至前面的岔路口位置,重新选择,直至遍历了所有的情况或者得到的预期最优解的情况下再结束. 与贪心算法不同的是,回溯算法理论上是可以得到最优解, ...

  7. 【例1】 0/1背包《信息学奥赛一本通》【解法一】 02

    /* [例1] 0/1背包<信息学奥赛一本通>[解法一] 02 http://ybt.ssoier.cn:8088/problem_show.php?pid=1267 */ #includ ...

  8. HDOJ 2602-Bone Collector(0/1背包模板、打印方案及滚动数组解法)

    0/1背包 一.Bone Collector 解法一:二维数组解法(0/1背包模板代码) 1.1 0/1背包打印方案代码 解法二:滚动数组(一维)解法 2.1 一维滚动数组例题 E-爱玩游戏的Tom ...

  9. 动态规划(五)——0/1背包

    0/1背包 一.0/1背包问题 1.实例讲解 2.DP求解0/1背包 3.输出0/1背包方案 二.0/1背包题目代码(持续更新) 一.0/1背包问题 给定n种物品和一个背包,物品i的重量为wi,价值为 ...

最新文章

  1. 解决Undefined function or method 'vgg_kmiter' for input arguments of type 'double'.
  2. [C#泛型系列文章]
  3. P3225 [HNOI2012]矿场搭建
  4. 使下拉框某项不可选的方法
  5. 状态压缩 DP AHU420
  6. Spring-Data-Jpa简介
  7. eclipse改变默认的编码格式(UTF-8)
  8. 【BZOJ5495】[十二省联考2019]异或粽子(主席树,贪心)
  9. 使用文本编辑器+命令行的方式实现Java中的第一个程序Hello World(下)
  10. 拖放drag drop(PyQt或Qt for python)
  11. [线性模型总结] 线性回归+方差分析+协方差分析+混合效应+面板数据模型
  12. CSS 如何完美地去除表格的 “双线”
  13. david lowe 论文_访谈:L. Lee Lowe-博客小说家
  14. 软件构造笔记——Rep Invariantand Abstraction Function
  15. 合肥工业大学计算机与信息学院导师介绍,合肥工业大学计算机与信息学院硕士生导师:方静副教授...
  16. Android 上的SSH软件 connectbot
  17. Android4.0 SDK新功能详解
  18. JavaScript中的强制多态 | Lynda教程 中文字幕
  19. 如何将微信聊天记录、转账记录作为打官司的证据
  20. 前端进击的巨人(一):执行上下文与执行栈,变量对象

热门文章

  1. Android及java中list循环添加时覆盖的问题-20171021
  2. Visual Studio——理解多字节编码与Unicode码
  3. Swagger UI 与SpringMVC的整合 II
  4. copy与mutableCopy的内存管理剖析
  5. WebDriver原理分析
  6. Javascript学习------内部对象 String Date event(重要)
  7. NSString ,NSMutableString用法以及一些常用方法
  8. debian6 xen4.0安装 guest半虚拟化--debootstrap安装
  9. 两个datatable之间的复制
  10. 有用就存档,没用就删除,当日清理当日的email