DP入门---Robberies
Description
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
Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .
Output
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
Sample Output
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; double dp[10005]; int main() { int T,N,sum,money[105]; double P,p[105]; scanf("%d",&T); while(T--) { sum=0; scanf("%lf%d",&P,&N); P=1-P; for(int i=0; i<N; i++) { int d; double f; scanf("%d%lf",&d,&f); money[i]=d; p[i]=1-f; sum+=d; } memset(dp,0,sizeof(dp)); dp[0]=1; for(int i=0; i<N; i++) for(int j=sum; j>=money[i]; j--) { dp[j]=max(dp[j],dp[j-money[i]]*p[i]); } for(int i=sum;i>=0;i--) if(dp[i]>=P) { printf("%d\n",i); break; } } return 0; }