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
 /*分析:01背包的概率问题
当前的概率基于前一种状态的概率,即偷n家银行而不被抓的概率等于偷n-1家银行不被转的概率乘以偷第n家银行不被抓的概率。
用dp[i]表示偷价值为 i 时不被抓的概率,则状态转移方程为:
dp[j] = max(dp[j] , dp[j-m[i]] * (1-p[i]));
自己写关键在01背包的转换,原意是提供银行个数和期望被捕概率,然后将每个银行的钱数和逃脱概率给出,
通过将总数当作背包大小,通过求最大逃脱概率当作最大价值(但是并不是求这个),最终通过从总钱数递减找到低于期望被捕概率第一项背包,
即为不被逮捕的所能强盗的最大钱数。*/
#include <stdio.h>
#include <string.h> int m[];
double p[],dp[]; int main()
{
int t,n,i,j,sum;
double P;
scanf("%d",&t);
while(t--)
{
scanf("%lf %d",&P,&n);
sum = ;
P = - P;
for(i = ; i < n; i++)
{
scanf("%d %lf",&m[i],&p[i]);
sum += m[i];
p[i] = - p[i];
}
memset(dp,,sizeof(dp));
dp[] = ;
for(i = ; i < n; i++)
for(j = sum; j >= m[i]; j--)
if(dp[j] < dp[j-m[i]]*p[i])
dp[j] = dp[j-m[i]]*p[i];
for(i = sum; i >= &&dp[i] < P; i--);
printf("%d\n",i);
}
return ;
}

Roy the Robber的更多相关文章

  1. Hdu 2955 Robberies 0/1背包

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

  2. DP专题训练之HDU 2955 Robberies

    打算专题训练下DP,做一道帖一道吧~~现在的代码风格完全变了~~大概是懒了.所以.将就着看吧~哈哈 Description The aspiring Roy the Robber has seen a ...

  3. HDU2955 Robberies[01背包]

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

  4. HDU 2955(0-1背包问题)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/M 题目: Description The aspir ...

  5. HDU2955 背包DP

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

  6. HDU 2955

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

  7. HDU 2955 01背包(思维)

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

  8. hdu 2955 01背包

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

  9. DP入门---Robberies

    HDU  2955 Description The aspiring Roy the Robber has seen a lot of American movies, and knows that ...

随机推荐

  1. Find Peak Element 解答

    Question A peak element is an element that is greater than its neighbors. Given an input array where ...

  2. C 本地文件夸网文件Cp操作

    1,linux平台C简单实现本地文件cp 码子及运行效果测试

  3. c++ windows下declspec

    一.declspec #ifdef STATIC_LIBS #define DLL_API static #else #define DLL_API __declspec (dllexport) #e ...

  4. mysqldump命令详解(转载)

    1.简介 mysqldump为MySQL逻辑备份工具,产生一系列SQL语句,之后重新执行以产生备份的库.表及数据.也可产生CSV.XML等格式的数据.适用于各类引擎的表. 运行mysqldump需一定 ...

  5. Socket通信原理和实践

    我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程怎么与web服务器通信的?当你用QQ聊天时,QQ进程怎么与服务器或你好友所在的QQ进程通信?这些都得靠so ...

  6. 给定N个整数集合是否存在两个其和刚好为指定常数的元素

    又一次学习一遍<算法导论>,看到了这个问题: 描写叙述一个执行时间为O(nlgn)的算法,使之能在给定一个由n个整数构成的集合S和还有一个整数 X 时,推断出S中是否存在有两个其和刚好等于 ...

  7. (HYSBZ)BZOJ 1588 营业额统计

    营业额统计 Time Limit: 5000MS   Memory Limit: 165888KB   64bit IO Format: %lld & %llu Description 营业额 ...

  8. Android日志系统驱动程序Logger源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6595744 我们知道,在Android系统中, ...

  9. jquery更新后怎样在一个站点中使用两个版本号的jQuery

    公司眼下的项目中的右側导航菜单用到了bootstrap(v3.2.0)的affix.js(Affix插件)与scrospy.js(滚动监听)插件, 须要用到版本号>= 1.9.0的jquery, ...

  10. windows下用vs2008和boost结合编译程序

      原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://co63oc.blog.51cto.com/904636/504469 win ...