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.<br><br><center><img src=../../../data/images/con211-1010-1.jpg></center> <br>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.<br><br><br>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 . <br>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.<br><br>Notes and Constraints<br> 0 < T <= 100<br> 0.0 <= P <= 1.0<br> 0 < N <= 100<br> 0 < Mj <= 100<br> 0.0 <= Pj <= 1.0<br> 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 和 Robber,已知银行所能抢到的最大钱数与不被抓住的几率,求在不被抓住的几率最大的情况下所能抢到的最大钱数。
思路:记录所有银行的钱数,以及被抓住的几率,转化为被抓住的情况下的最大几率的钱数,最后再转化为不被抓到的情况。
感悟:有些难度,但还可以解决。
代码:
<span style="font-size:14px;">#include<iostream>
#include<algorithm>
using namespace std;
int m[10010];
double p[10010];
double max(double a,double b)
{
return a>b?a:b;
}
int main()
{
int n,t;
double p1;
cin>>t;
while(t--)
{
cin>>p1>>n;
p1=1-p1;
int i,j,sum=0;
for(i=0;i<n;i++)
{
cin>>m[i]>>p[i];
p[i]=1-p[i];
sum+=m[i];
}
double dp[10005]={1.0};
for(i=0;i<n;i++)
{
for(j=sum;j>=m[i];j--)
{
dp[j]=max(dp[j],dp[j-m[i]]*p[i]);
}
}
for(i=sum;i>=0;i--)
{
if(dp[i]-p1>1e-9)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}</span>