HDUOJ---2955 Robberies

Robberies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9563    Accepted Submission(s): 3575


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.

HDUOJ---2955 Robberies

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
 

 

Source
 

 思路: 这道题可以用o/1背包来解答,

   思路是将被捕的概率转变为escape的概率,escape=1-catch;

所以抢一个bank,然后都能escape,则抢掉n个bank,逃跑的概率为  tol_escape=escape1*escape2*.....;

这样就可以求出最多能抢到的money啦....

讲的,若果还不明白的话,就再开下代码吧,估计既可以百分百理解了...

 1 #include<stdio.h>

 2 #include<string.h>

 3 #include<stdlib.h>

 4 const int maxn=101;

 5 struct bank

 6 {

 7     int m;

 8     float p;

 9 };

10 bank sta[101];

11 float dp[10001];

12 int main()

13 {

14     int test;

15     int toln,i,j,toll;

16     float tolp;

17     scanf("%d",&test);

18     while(test--)

19     {

20         scanf("%f%d",&tolp,&toln);

21         tolp=1-tolp;

22         toll=0;

23         for(i=0;i<toln;i++)

24         {

25             scanf("%d%f",&sta[i].m,&sta[i].p);

26             sta[i].p=1-sta[i].p;       // scape

27             toll+=sta[i].m;            //得到总容量

28         }

29         memset(dp,0,sizeof(dp));

30         dp[0]=1;

31         for(i=0;i<toln;i++)

32         {

33             for(j=toll;j>=sta[i].m;j--)

34             {

35                 if(dp[j]<dp[j-sta[i].m]*sta[i].p)

36                 {

37                    dp[j]=dp[j-sta[i].m]*sta[i].p;

38                 }

39             }

40         }

41         int ans=0;

42         for(j=toll;j>=0;j--)

43         {

44             if(dp[j]>=tolp)

45             {

46                 ans=j;

47                 break;

48             }

49         }

50         printf("%d\n",ans);

51     }

52 

53 

54     return 0;

55 }
View Code

 

 

你可能感兴趣的:(HDU)