题目链接:https://vjudge.net/problem/LightOJ-1079

1079 - Just another Robbery
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点,可直接背包求解。

代码一:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e4+; double dp[MAXN];
int main()
{
int T, n, kase = ;
scanf("%d", &T);
while(T--)
{
double P;
scanf("%lf%d", &P, &n);
for(int j = MAXN-; j>=; j--) dp[j] = ;
dp[] = ;
for(int i = ; i<=n; i++)
{
int val; double pa;
scanf("%d%lf", &val,&pa);
for(int j = MAXN-; j>=val; j--)
dp[j] = min(dp[j], dp[j-val]+pa-dp[j-val]*pa);
} int k;
for(k = MAXN-; k>=; k--)
if(dp[k]<=P) break;
printf("Case %d: %d\n", ++kase, k);
}
}

代码二:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e4+; double dp[MAXN];
int main()
{
int T, n, kase = ;
scanf("%d", &T);
while(T--)
{
double P;
scanf("%lf%d", &P, &n);
memset(dp, , sizeof(dp));
dp[] = ;
for(int i = ; i<=n; i++)
{
int val; double pa;
scanf("%d%lf", &val,&pa);
for(int j = MAXN-; j>=val; j--)
dp[j] = max(dp[j], dp[j-val]*(-pa));
} int k;
for(k = MAXN-; k>=; k--)
if(-dp[k]<=P) break;
printf("Case %d: %d\n", ++kase, k);
}
}

最新文章

  1. 解决问题:无法对 System程序集 添加Fakes程序集
  2. 上个项目的一些反思 I
  3. C/C++面试题总结
  4. MySQL中GROUP_CONCAT中排序
  5. 认识SQLServer索引以及单列索引和多列索引的不同
  6. SQL总结(五)存储过程
  7. 实现IOS圆角风格的列表ListView
  8. 关系数据库&amp;amp;&amp;amp;NoSQL数据库
  9. oc_转_NSInteger 和 NSNumber
  10. Java笔试知识总结(第一回)
  11. js获得url内的参数
  12. Android之drawable state各个属性具体解释
  13. update与fixedupdate差别
  14. vue中的 ref 和 $refs
  15. create table as 和create table like的区别
  16. MySql cmd下的学习笔记 —— 有关表的操作(对表中数据的增,删,改,查)
  17. nohup.out文件过大解决方法 定时任务清空
  18. CSRF理解和实战
  19. Android常用学习网站
  20. 【BZOJ1044】[HAOI2008]木棍分割(动态规划,贪心)

热门文章

  1. MySQL经常使用命令--create命令使用
  2. Testin云測手游质量管家 七大兵器助CP称霸江湖
  3. OLR
  4. PHP之面向对象学习
  5. 【Excle数据透视表】如何重命名数据透视表
  6. MySqlDataReader
  7. Time倒计时
  8. com.opensymphony.xwork2.ognl.OgnlValueStack - Error setting expression &#39;customer.applyRate&#39; with value &#39;[Ljava.lang.String;@7d3fae2c&#39;
  9. eval(function(p,a,c,k,e,d){e=function(c)加解密
  10. hadoop 相关工具访问端口(转)