Robberies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19862    Accepted Submission(s): 7344

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
 

这题很久以前做过,当时不是很懂,看着别人的思路才写出来的,而且不是很懂思路中的转换,所以这次重新做一遍。

思路:题目中给定价值和被抓几率,但是被抓几率不可以用乘积来组合计算,举个例子,比如第一个银行3%被抓几率,第二个5%被抓几率,那么乘起来会变成0.15%,抢的越多,被抓几率却越小了,显然不对,因此要转换成不被抓几率,上述例子则变为第一家97%不被抓,第二家95%不被抓,乘起来就是92.15%,抢的越多,不被抓的几率越来越小即被抓几率越来越大,这样才是符合常理的。

那么背包体积应该是什么呢?先看最普通01背包,用数个cost来填充V,使得value之和尽量大,那么这题就应该是用数个money填充总money,使得不被抓几率尽量大。那转移方程就是dp[j]=max(dp[j],dp[j-w]*c),这里和01背包的区别就是从+改成了*。然后得到dp数组是0~V情况下的不被抓几率的最优(大)值,这根答案有什么关系?逆序枚举每一种情况,若此情况下的dp值即不被抓几率大于等于题目中所给的不被抓几率,那就输出,逆序着从大到小枚举保证了找到的一个解是最优解。

代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdlib>
  4. #include<sstream>
  5. #include<cstring>
  6. #include<cstdio>
  7. #include<string>
  8. #include<deque>
  9. #include<stack>
  10. #include<cmath>
  11. #include<queue>
  12. #include<set>
  13. #include<map>
  14. using namespace std;
  15. #define INF 0x3f3f3f3f
  16. #define MM(x) memset(x,0,sizeof(x))
  17. #define MMINF(x) memset(x,INF,sizeof(x))
  18. typedef long long LL;
  19. const double PI=acos(-1.0);
  20. const int N=110;
  21. struct info
  22. {
  23. int w;
  24. double c;
  25. };
  26. info bank[N];
  27. double dp[N*100];
  28. bool cmp(const info &a,const info &b)
  29. {
  30. return a.w<b.w;
  31. }
  32. int main(void)
  33. {
  34. int tcase,i,j,V,n;
  35. double k;
  36. scanf("%d",&tcase);
  37. while (tcase--)
  38. {
  39. V=0;
  40. scanf("%lf%d",&k,&n);
  41. k=1.0-k;
  42. for (i=0; i<n; i++)
  43. {
  44. scanf("%d%lf",&bank[i].w,&bank[i].c);
  45. bank[i].c=1.0-bank[i].c;
  46. V+=bank[i].w;
  47. }
  48. sort(bank,bank+n,cmp);
  49. for (i=0; i<N*100; i++)
  50. dp[i]=0;
  51. dp[0]=1;
  52. for (i=0; i<n; i++)
  53. {
  54. for (j=V; j>=bank[i].w; j--)
  55. {
  56. dp[j]=max(dp[j],dp[j-bank[i].w]*bank[i].c);
  57. }
  58. }
  59. for (i=V; i>=0; i--)
  60. {
  61. if(dp[i]>=k)
  62. {
  63. printf("%d\n",i);
  64. break;
  65. }
  66. }
  67. }
  68. return 0;
  69. }

HDU——2955Robberies(小数背包)的更多相关文章

  1. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  2. hdu 5445 多重背包

    Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  3. hdu 1203 01背包 I need a offer

    hdu 1203  01背包  I need a offer 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 题目大意:给你每个学校得到offe ...

  4. hdu 1864 01背包 最大报销额

    http://acm.hdu.edu.cn/showproblem.php?pid=1864 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...

  5. HDU 1203 01背包变形题,(新思路)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1203 I NEED A OFFER! Time Limit: 2000/1000 MS (Java/ ...

  6. HDU 1712 分组背包

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. hdu 2191 多重背包 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

    http://acm.hdu.edu.cn/showproblem.php?pid=2191 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...

  8. hdu 2844 多重背包coins

    http://acm.hdu.edu.cn/showproblem.php?pid=2844 题意: 有n个硬币,知道其价值A1.....An.数量C1...Cn.问在1到m价值之间,最多能组成多少种 ...

  9. hdu 2955 01背包

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...

随机推荐

  1. Ubuntu16.04 + cuda8.0 + GTX1080安装教程

    1. 安装Ubuntu16.04 不考虑双系统,直接安装 Ubuntu16.04,从 ubuntu官方 下载64位版本: ubuntu-16.04-desktop-amd64.iso . 在MAC下制 ...

  2. 4G牌照影响

    与3G牌照发放整整讨论了10年不同,4G牌照发放在2009年3G规模建设4年后就进行了发放,也颇匹配于行业的加速度.那么,4G到底会在哪些方面.在何种程度上改变中国呢?其实,4G的影响可能没有那么大, ...

  3. jmeter常量吞吐量定时器

    jmeter常量吞吐量定时器

  4. 运行powershell 脚本 在此系统上禁止运行脚本

    解决方法: 首次在计算机上启动 Windows PowerShell 时,现用执行策略很可能是 Restricted(默认设置). Restricted 策略不允许任何脚本运行. 若要了解计算机上的现 ...

  5. HDOJ 4509 湫湫系列故事——减肥记II(2013腾讯编程马拉松) 并查集合并区间

    发现这种合并区间的题目还可以这么玩 给你n段时间 然后问没被占用的时间是多少 题目所给的区间是右开的导致我wa 好多人5e5*1440的暴力跑出来的时间居然只是我的两倍 不懂.... 所以并查集并没有 ...

  6. 多源最短路径 – Floyd-Warshall Algorithm

    介绍: 是解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权(但不可存在负权回路)的最短路径问题,同时也被用于计算有向图的传递闭包. Floyd-Warshall算法的时间复杂度是O(N3) ...

  7. 2010: C语言实验——逆置正整数

    2010: C语言实验——逆置正整数 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 949  Solved: 691[Submit][Status][We ...

  8. input提示文字;placeholder字体修改

    在很多网站上我们都看到input输入框显示提示文字,让我们一起来看看如果在input输入框中显示提示文字.我们只需要在<input>标签里添加:placeholder="提示文字 ...

  9. Android Studio 中安装 apk 被拆分成多个 slice,如何禁止?

    Android Studio 3.0.1 中,Run 'app' 时,生成的 apk 被分割成多个 slice: $ adb install-multiple -r D:\...\app\build\ ...

  10. Kernel Stack Overflow(转)

    0x00 漏洞代码 stack_smashing.c #include <linux/init.h> #include <linux/module.h> #include &l ...