A - Robberies

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description:

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

  1. 3
  2. 0.04 3
  3. 1 0.02
  4. 2 0.03
  5. 3 0.05
  6. 0.06 3
  7. 2 0.03
  8. 2 0.03
  9. 3 0.05
  10. 0.10 3
  11. 1 0.03
  12. 2 0.02
  13. 3 0.05
 

Sample Output

2
4
6
这是一道01背包的问题,但是有一个地方我没有想到,所以花了很多时间。

  题目给了每个银行的钱和被抓的概率,由于要抢尽量多的钱,所以要保证尽量不被抓,而抢多个银行之后不被抓的概率是抢每一个银行不被抓的概率之 积,我竟然把这一点给忘了!导致我走了许多弯路,思路不能太死啊!dp[]表示抢到下标所对应的钱时,此时不被抓的概率,题目给出了最终不能高于被抓概率 P,不被抓的概率就不能低于(1-P),所以最后只需要逆序遍历dp,找到第一个大于等于1-P的dp[i],就能够保证i最大,即抢到的钱最多。

  1. /* ***********************************************
  2. Author :Mubaixu
  3. File Name :HDU2955.cpp
  4. ************************************************ */
  5.  
  6. #include<stdio.h>
  7. #include<string.h>
  8. #include<iostream>
  9. #include<queue>
  10. #include<algorithm>
  11. using namespace std;
  12. const int maxm=;
  13. const int maxn=;
  14. double dp[maxm];
  15. int value[maxn];
  16. double tp[maxn];
  17. int main(){
  18. int t;
  19. int n;
  20. double p;
  21. scanf("%d",&t);
  22. while(t--){
  23. scanf("%lf%d",&p,&n);
  24.  
  25. int sum=;
  26. for(int i=;i<=n;i++){
  27. scanf("%d%lf",&value[i],&tp[i]);
  28. tp[i]=-tp[i];
  29. sum+=value[i];
  30. }
  31. memset(dp,,sizeof(dp));
  32. dp[]=;
  33.  
  34. for(int i=;i<=n;i++){
  35. for(int j=sum;j>=value[i];j--){
  36. dp[j]=max(dp[j],dp[j-value[i]]*tp[i]);
  37. }
  38. }
  39. for(int i=sum;i>=;i--){
  40. if(dp[i]>(-p)){
  41. printf("%d\n",i);
  42. break;
  43. }
  44.  
  45. }
  46.  
  47. }
  48. return ;
  49. }

HDU 2955 Robberies 背包概率DP的更多相关文章

  1. HDU 2955 Robberies(概率DP,01背包)题解

    题意:给出规定的最高被抓概率m,银行数量n,然后给出每个银行被抓概率和钱,问你不超过m最多能拿多少钱 思路:一道好像能直接01背包的题,但是有些不同.按照以往的逻辑,dp[i]都是代表i代价能拿的最高 ...

  2. hdu 2955 Robberies(概率背包)

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

  3. hdu 2955 Robberies 背包DP

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

  4. HDU 2955 Robberies(DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=2955 题目: Problem Description The aspiring Roy the Rob ...

  5. HDU 4089 Activation(概率DP)(转)

    11年北京现场赛的题目.概率DP. 公式化简起来比较困难....而且就算结果做出来了,没有考虑特殊情况照样会WA到死的.... 去参加区域赛一定要考虑到各种情况.   像概率dp,公式推出来就很容易写 ...

  6. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

  7. HDU - 5001 Walk(概率dp+记忆化搜索)

    Walk I used to think I could be anything, but now I know that I couldn't do anything. So I started t ...

  8. hdu 2955 Robberies 0-1背包/概率初始化

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

  9. hdu 2955 Robberies(背包DP)

    题意: 小偷去抢银行,他母亲很担心. 他母亲希望他被抓的概率真不超过P.小偷打算去抢N个银行,每个银行有两个值Mi.Pi,Mi:抢第i个银行所获得的财产 Pi:抢第i个银行被抓的概率 求最多能抢得多少 ...

随机推荐

  1. [USACO 1.5.4]checker(水题重做——位运算(lowbit的应用))

    描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 0 1 2 3 4 5 6 ------- ...

  2. js中的DOM操作(2)

    1.表格的更加与删除 <!DOCTYPE html> <html> <head> <title>表格操作</title> <style ...

  3. Linq之Linq to Sql

    目录 写在前面 系列文章 Linq to sql 总结 写在前面 上篇文章介绍了linq to xml的相关内容,linq to xml提供一种更便捷的创建xml树,及查询的途径.这篇文章将继续介绍l ...

  4. AngularJS开发指南1:AngularJS简介

    什么是 AngularJS? AngularJS 是一个为动态WEB应用设计的结构框架.它能让你使用HTML作为模板语言,通过扩展HTML的语法,让你能更清楚.简洁地构建你的应用组件.它的创新点在于, ...

  5. websocket在.net4.5中实现的简单demo

    以下代码环境要求:win8或win10, .net4.5+IIS8 win7上是IIS7,win7上.net本身不直接支持websocket, win7可以用superwebsocket, 或自己根据 ...

  6. Spring security 学习 (自助者,天助之!)

    自己努力,何必要强颜欢笑的求助别人呢?  手心向下不求人! Spring security学习有进展哦: 哈哈! 1.页面都是动态生产的吧! 2.设置权限:  a:pom.xml配置jar包 b:cr ...

  7. 【BZOJ 3545】【ONTAK 2010】Peaks & 【BZOJ 3551】【ONTAK 2010】Peaks加强版 Kruskal重构树

    sunshine的A题我竟然调了一周!!! 把循环dfs改成一个dfs就可以,,,我也不知道为什么这样就不会RE,但它却是A了,,, 这周我一直在调这个题,总结一下智障错误: 1.倍增的范围设成了n而 ...

  8. hdu1520 树形dp

    树形dp入门,在树上进行dp. 状态转移方程: dp[i][0] = max(dp[j][0], dp[j][1]);//i为j的上司 并且i不来 dp[i][1] = dp[j][0];//i来了 ...

  9. 【HDU 5578】Friendship of Frog

    题 题意 求相同字母最近距离 分析 用数组保存各个字母最后出现的位置,维护最小距离. 代码 #include <cstdio> int c[30],n,p,a,minl; char ch; ...

  10. CoreOS Architecture Learning

    目录 . CoreOS简介 . CoreOS部署.安装.使用 . CoreOS命令使用 1. CoreOS简介 0x1: CoreOS和Docker的关系 我们先来看一张Docker的架构图