M - 01背包

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

 

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被抓的最大概率P,求他在不被被抓的情况下,抢劫的钱最多。

        被抓概率为pj,安全概率1-pj,他的安全概率大于1-P时都是安全的。抢劫的金额为0时,肯定是安全的,所以dp[0]=1;其他金额初始为最危险的所以概率全为0;
思路:
        和背包问题做一个简单的转化:把每个银行的储钱量之和total当成背包容量C,然后不被抓的概率1-pj当成价值w来求。那么他的最大的不被抓的概率下抢劫的钱最多的情况就是乘积了,最后再枚举一下就行了,为什么要用乘积呢这里,想一想数学中涉及到概率的问题,而不是简单概率的相加吧,。这里计算的是抢劫到j钱不被抓的概率,相乘才能保存下来。

dp[ j] 表示在抢劫到钱j时不被抓概率。最后,只要找出满足要求的最大j。

代码及案例显示

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int T,n,total;
double p;
double a[],dp[];
int b[];
int main()
{
cin>>T;
while(T--)
{
total=;
cin>>p>>n;
for(int i=; i<n; i++)
{
cin>>b[i]>>a[i];
total+=b[i];
}
memset(dp,,sizeof(dp));
dp[]=; //没抢到钱时,逃脱的概率为1
for(int i=; i<n; i++)
for(int j=total; j>=a[i]; j--)
{
dp[j]=max(dp[j],dp[j-b[i]]*(-a[i]));
//cout<<d[j]<<" "<<j<<endl;
}
for(int i=total; i>=; i--)
{
if(dp[i]>-p)
{
cout<<i<<endl;
break;
}
}
}
return ;
}

下面是第一组案例的各种抢劫钱数的概率

HDU 2955(01背包问题)的更多相关文章

  1. hdu 2955 01背包

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

  2. HDU 1171 (01背包问题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 分析: 例如数据 3 10    2 20    1 30    1 获得这样一个降序的数组: ...

  3. HDU 2955 01背包(思维)

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

  4. Robberies hdu 2955 01背包

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

  5. HDU 2546 01背包问题

    这里5元是个什么意思呢.差不多就是特殊情况了. 就是说最贵的那个东西先不买.并且最后要留下5元去买那个最贵的. 也就是说对现在金钱-5 拿剩下的钱去对减去最贵的商品后的商品dp.看这些剩下的钱能买多少 ...

  6. HDU _2546 01背包问题

    A - 饭卡 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  7. HDU 1864最大报销额 01背包问题

    B - 最大报销额 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  8. HDU 2602 Bone Collector(经典01背包问题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/O ...

  9. HDU 3466 Proud Merchants 带有限制的01背包问题

    HDU 3466 Proud Merchants 带有限制的01背包问题 题意 最近,伊萨去了一个古老的国家.在这么长的时间里,它是世界上最富有.最强大的王国.因此,即使他们的国家不再那么富有,这个国 ...

  10. HDU 2602 Bone Collector (01背包问题)

    原题代号:HDU 2602 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 原题描述: Problem Description Many yea ...

随机推荐

  1. uoj#67. 新年的毒瘤(割顶)

    #67. 新年的毒瘤 辞旧迎新之际,喜羊羊正在打理羊村的绿化带,然后他发现了一棵长着毒瘤的树. 这个长着毒瘤的树可以用n个结点m 条无向边的无向图表示.这个图中有一些结点被称作是毒瘤结点,即删掉这个结 ...

  2. codevs3945 完美拓印

    3945 完美拓印 codevs月赛 第一场 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 小Q获得了一个神奇的印章,这个印章宽n ...

  3. BFS zoj 1649

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...

  4. 《A First Course in Probability》-chape6-随机变量的联合分布-基本概念

    之前我们探讨了一元随机变量的分布列,下面我们应该将相应的性质推广到多元随机变量的分布列,在这里我们主要以讨论二元随机变量分布列为主. 利用类比的方法,我们很容易将一元随机变量的分布列的性质推广上来. ...

  5. 《A First Course in Probability》-chaper5-连续型随机变量-正态分布

    古典统计学问题一开始起源于赌博,让我们看这样一道有关赌博的问题. Q:A.B两人进行n局赌博,A胜的概率是p,现在设置随机变量X表示A赢的局数,当X>np,A给赌场X-np元,否则B给赌场np- ...

  6. Best Reward HDU 3613(回文子串Manacher)

    题目大意:有一个串(全部由小写字母组成),现在要把它分成两部分,如果分开后的部分是回文串就计算出来它的价值总和,如果不是回文的那么价值就是0,最多能得到的最大价值.   分析:首先的明白这个最大价值有 ...

  7. zzuoj 10408: C.最少换乘【最短路dijkstra】

    10408: C.最少换乘 Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 31  Solved: 8[Submit][Status][Web Board ...

  8. [置顶] VC++界面编程之--使用分层窗口实现界面皮肤

    使用分层界面来实现界面皮肤的好处是:可以保证图片边缘处理不失真,且能用于异形窗口上,如一些不规则的窗口,你很难用SetWindowRgn来达到理想效果. 在很多情况下,界面的漂亮与否,取决于PS的制作 ...

  9. 转载:Ununtu下中文乱码解决方案

    转载: 添加中文字符编码: $sudo vim /var/lib/locales/supported.d/local #添加下面的中文字符集 zh_CN.GBK GBK zh_CN.GB2312 GB ...

  10. Python中 os._exit() sys.exit() exit()区别

    Python退出程序的方式有两种:os._exit(), sys.exit() 1)os._exit() 直接退出 Python程序,其后的代码也不会继续执行. 2)sys.exit() 引发一个 S ...