Robberies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12908 Accepted Submission(s): 4774
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
Source
IDI Open 2009
#include<stdio.h>
#include<string.h>
#include<math.h>
#define max(A,B) (A)>(B)?(A):(B)
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
double V, f[11000], va[110];//100个数每个数最大为100
int vo[110], n, sum = 0;
scanf("%lf%d", &V, &n);
V = 1.0 - V;
for(int i = 1; i <= n; i++)
{
scanf("%d%lf", &vo[i], &va[i]);//将整数看作容量,浮点数看作价值
va[i] = 1.0 - va[i];
sum += vo[i];//保存最大容量
}
memset(f, 0, sizeof(f));
f[0] = 1;//反过来初始化为1 便于后面的乘法递推
for(int i = 1; i <= n; i++)
{
for(int j = sum; j >= vo[i]; j--)
{
f[j] = max(f[j], f[j-vo[i]] * va[i]);//记录所有浮点数所对应的容量
}
}
int i;
for( i = sum; i >= 0; i--)
if(f[i] >= V)//如果遇到一个浮点数大于给定值那么当前i<span style="font-family: Arial, Helvetica, sans-serif;">就是最大容量
</span><span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>printf("%d\n",i);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return 0;
}
<span style="font-family: Arial, Helvetica, sans-serif;"></span>
题意:小偷抢银行,每个银行都有一定的钱,并且每偷一个银行小偷都会有一个被抓的概率值,给定一个概率值,在这个概率值一下小偷是安全的,求小偷所能偷到的最大钱数。
思路:本题由于概率值是浮点数,所以必须采用逆向思维,将浮点数看作是价值,钱数看作是容量,求得时候就求所得到的一系列浮点数值中那个最接近给定值的容量就是答案。
关键:这里每个银行的概率是独立的,所以求下一个银行被抓时的概率得用乘法。其次这里由于数据给的浮点数特别小,所以反面取浮点数的值,便于计算。还有注意初始化的值。