Robberies
Robberies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15211 Accepted Submission(s): 5577
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.
![](http://acm.hdu.edu.cn/data/images/con211-1010-1.jpg)
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.
![](http://acm.hdu.edu.cn/data/images/con211-1010-1.jpg)
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 .
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.
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
Source
Recommend
题目大意:
Roy 闲的蛋疼,想要抢银行,给你Roy 抢银行的所能够接受的被抓的最大概率,下面有N行,每一行输入该银行的金钱,和在该银行被抓到的概率。
问你Roy ,在满足其被抓到的概率小于其所能够接受被抓的最大概率的情况下,所能够获取到得最大金钱。
PS:注意被抓到的概率,如果是多银行的话,要注意被抓到的概率是指在所获取金钱的银行中都没有被抓到的概率,而且,每一个银行被抓到的概率都是相互独立的。
解法:
如果直接按照被抓的最大概率去做的话,会比较麻烦些(难点),不妨他被抓的概率转化为安全概率,既然为P=1-P;
然后可以把该问题转化为背包问题,吧所能够获取的金钱看做背包的大小,其每次DP[i]保存的为获取i数额的金钱,所能达到的最大安全概率。
再去判断安全概率大与其最小的安全概率(既为他所能够接受的被抓的最大概率转化为安全概率)即可,需要注意的还有一点是DP[0]=1,表示没抢金币,安全率为1、
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 double DP[100010];/*DP[i]表示,获取金钱i时,任不被抓到的安全概率*/ 6 double P[100010];/*在第i个银行不被抓到得安全概率概率*/ 7 int V[100010]; /*抢劫第i个银行所能够获取的金钱*/ 8 int main() 9 { 10 int T,i,j; 11 scanf("%d",&T); 12 while(T--) 13 { 14 int N,Sum_Money=0; 15 double Max,sign; 16 scanf("%lf%d",&Max,&N); 17 Max=1-Max; /*转化为安全的概率*/ 18 for(i=1;i<=N;i++) 19 { 20 scanf("%d%lf",&V[i],&P[i]); 21 P[i]=1-P[i]; /*转化为安全概率*/ 22 Sum_Money+=V[i];/*获取金钱总数*/ 23 } 24 for(i=0;i<=Sum_Money;i++)DP[i]=0; 25 for(i=1,DP[0]=1;i<=N;i++)/*处理DP[i]中的概率*/ 26 for(j=Sum_Money;j>=V[i];j--) 27 DP[j]=max(DP[j-V[i]]*P[i],DP[j]); 28 for(i=Sum_Money;i>=0;i--)/*从大到小遍历找符合条件的最大值*/ 29 if( DP[i]>=Max){printf("%d\n",i);break;} 30 } 31 return 0; 32 }
转载请备注:
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************