robberies
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 20669 Accepted Submission (s): 7656
Problem Description The aspiring Roy the robber have seen a lot of American movies, and knows so the bad guys usually get S 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 had 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, had decided upon a tolerable probability of getting caught. She feels that he's 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 is 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's Pj.
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 your may assume that all probabilities be independent as the police have very lo W 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
Test Instructions: There is a thief to steal money from the bank, but he stole every bank always have a certain probability of being caught, now give you a probability p, asked to ensure that he in a safe situation, he can steal the maximum amount of money.
The topic of the probability, and then test the probability of the sample is two decimal places, that is not to show that all the test data are two decimal places, we can give the probability of *100, and then converted to the simplest 01 backpack it.
Unfortunately, after my testing, this idea is wrong because the number of decimals in the test data is more than two bits.
then we'll have to change the idea. Given the probability that P is the probability of his being caught in the bank, then (1-p) is the probability of its escape.
The probability of his total escape through all the banks is the product of all probability of escape. In this case, we can think of the total amount of money in all banks as the capacity of the backpack, the probability of escape as a value, then the same is a 01 knapsack problem, but the 01 backpack calculates its maximum escape probability.
The state transition equation is: Dp[j]=max (dp[j],dp[j-a[i].vi]* (1.0-A[I].P));
then after the two-cycle DP, we can calculate all the possible amount of money in the escape probability, and then only need to start from the total sum of money enumerated, as long as the probability of escape to meet the second line of input probability is OK.
AC Code:
#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h
> #define MAX (A, B) (A>B?A:B) using namespace std;
struct PO {int VI;
Double p;
} a[10010];
Double dp[10010];
int main () {int T;
scanf ("%d", &t);
Double m,n;
while (t--) {memset (dp,0,sizeof (DP));
scanf ("%lf%lf", &m,&n);
int sum=0;
for (int i=0; i<n; i++) {scanf ("%d%lf", &A[I].VI,&A[I].P);
SUM+=A[I].VI;
} dp[0]=1; for (int i=0, i<n; i++) for (int j=sum; j>=a[i].vi; j--) Dp[j]=max (dp[j],dp[j-a[i].vi]* (1.
0-A[I].P));
for (int i=sum; i>=0; i--) if (dp[i]>1-m) {printf ("%d\n", I);
Break
}} return 0; }