题目链接:https://vjudge.net/problem/LightOJ-1079
| Time Limit: 4 second(s) | Memory Limit: 32 MB |
As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - Hermione and Ron have decided upon a tolerable probability P of getting caught. They feel that he is safe enough if the banks he robs together give a probability less than P.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains a real number P, the probability Harry needs to be below, and an integer N (0 < N ≤ 100), the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj (0 < Mj ≤ 100) and a real number Pj . Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj. A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.
Output
For each case, print the case number and the maximum number of millions he can expect to get while the probability of getting caught is less than P.
Sample Input | Output for 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 | Case 1: 2 Case 2: 4 Case 3: 6 |
题意:
有n家银行,xx准备打劫银行。每一家银行都有其价值以及被抓概率。在被抓概率不大于P的情况下,打劫那些银行收获最大?打劫每一家银行被抓的事件相互独立。
题解:
1.设dp[i]为收获i元时最小的被抓概率。
2.由于事件独立,即:P(AB) = P(A)P(B),因此:P(A∪B) = P(A) + P(B) - P(AB) = P(A) + P(B) - P(A)P(B) 。
3.根据第2点,可直接背包求解。
代码一:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int MOD = 1e9+7; 17 const int MAXN = 1e4+100; 18 19 double dp[MAXN]; 20 int main() 21 { 22 int T, n, kase = 0; 23 scanf("%d", &T); 24 while(T--) 25 { 26 double P; 27 scanf("%lf%d", &P, &n); 28 for(int j = MAXN-1; j>=1; j--) dp[j] = 1; 29 dp[0] = 0; 30 for(int i = 1; i<=n; i++) 31 { 32 int val; double pa; 33 scanf("%d%lf", &val,&pa); 34 for(int j = MAXN-1; j>=val; j--) 35 dp[j] = min(dp[j], dp[j-val]+pa-dp[j-val]*pa); 36 } 37 38 int k; 39 for(k = MAXN-1; k>=0; k--) 40 if(dp[k]<=P) break; 41 printf("Case %d: %d\n", ++kase, k); 42 } 43 }
代码二:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int MOD = 1e9+7; 17 const int MAXN = 1e4+100; 18 19 double dp[MAXN]; 20 int main() 21 { 22 int T, n, kase = 0; 23 scanf("%d", &T); 24 while(T--) 25 { 26 double P; 27 scanf("%lf%d", &P, &n); 28 memset(dp, 0, sizeof(dp)); 29 dp[0] = 1; 30 for(int i = 1; i<=n; i++) 31 { 32 int val; double pa; 33 scanf("%d%lf", &val,&pa); 34 for(int j = MAXN-1; j>=val; j--) 35 dp[j] = max(dp[j], dp[j-val]*(1-pa)); 36 } 37 38 int k; 39 for(k = MAXN-1; k>=0; k--) 40 if(1-dp[k]<=P) break; 41 printf("Case %d: %d\n", ++kase, k); 42 } 43 }
本文探讨了在《Harry Potter》系列结束后,Harry Potter决定打劫银行以快速赚钱的问题。通过动态规划算法,解决在被抓概率不超过特定值的情况下,如何选择打劫哪些银行以获得最大收益。文章提供了两种代码实现方式。
259

被折叠的 条评论
为什么被折叠?



