LightOJ - 1079 Just another Robbery —— 概率、背包
题目链接: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点,可直接背包求解。
代码一:
#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);
}
}
LightOJ - 1079 Just another Robbery —— 概率、背包的更多相关文章
- LightOJ 1079 Just another Robbery 概率背包
Description As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (h ...
- LightOJ 1079 Just another Robbery (01背包)
题意:给定一个人抢劫每个银行的被抓的概率和该银行的钱数,问你在他在不被抓的情况下,能抢劫的最多数量. 析:01背包,用钱数作背包容量,dp[j] = max(dp[j], dp[j-a[i] * (1 ...
- LightOJ 1079 Just another Robbery (01背包)
题目链接 题意:Harry Potter要去抢银行(wtf???),有n个银行,对于每个银行,抢的话,能抢到Mi单位的钱,并有pi的概率被抓到.在各个银行被抓到是独立事件.总的被抓到的概率不能超过P. ...
- LightOJ-1079-Just another Robbery(概率, 背包)
链接: https://vjudge.net/problem/LightOJ-1079#author=feng990608 题意: As Harry Potter series is over, Ha ...
- lightoj 1079 Just another Robbery
题意:给出银行的个数和被抓概率上限.在给出每个银行的钱和抢劫这个银行被抓的概率.求不超过被抓概率上线能抢劫到最多的钱. dp题,转移方程 dp[i][j] = min(dp[i-1][j] , dp[ ...
- (概率 01背包) Just another Robbery -- LightOJ -- 1079
http://lightoj.com/volume_showproblem.php?problem=1079 Just another Robbery As Harry Potter series i ...
- 1079 - Just another Robbery
1079 - Just another Robbery PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 3 ...
- LightOJ - 1079 概率dp
题意:n个银行,每个有价值和被抓概率,要求找被抓概率不超过p的最大价值 题解:dp[i][j]表示前i个取j价值的所需最小概率,01背包处理,转移方程dp[i][j]=min(dp[i-1][j],d ...
- hdu 2955 Robberies(概率背包)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- 转: Linux下使用java -jar运行可执行jar包的正确方式
from: http://codepub.cn/2016/05/11/The-correct-way-to-use-java-jar-run-an-executable-jar-package-un ...
- Another unnamed CacheManager already exists in the same VM
今天学习Spring 缓存机制.遇到不少问题~ 好不easy缓存的单元測试用例调试成功了,在同一项目下单元測试另外一个文件时,发生了异常: org.springframework.beans.fact ...
- 1毛钱的CDN你家的站点会用吗?
在第七届中国云计算大会上,作为CDN领域最具重量级的受邀发言人.迅雷CTO.网心科技CEO陈磊在发表重要演讲时,宣布迅雷将推出国内首家无限节点CDN.而这一款CDN号称眼下国内最廉价的CDN,售价仅为 ...
- dockerfile VOLUME 对外暴露目录设置问题
最近遇到一个特殊问题, 容器对外映射挂载目录 , 发现容器中的目录并没有映射到宿主机上. 后经排查是是镜像中没有指定 volume # 例如 FROM debian:wheezy VOLUME /da ...
- project 的用法
任务和子任务,树状结构: 点击一个绿色的箭头就可以实现. 时间的话:视图→甘特图→双击“开始时间”修改即可
- bzoj1061【NOI2008】志愿者招募
1061: [Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 2740 Solved: 1703 [Submit][id ...
- 『HTML5梦幻之旅』 - 仿Qt演示样例Drag and Drop Robot(换装机器人)
起源 在Qt的演示样例中看到了一个有趣的demo.截图例如以下: 这个demo的名字叫Drag and Drop Robot,简单概括而言,在这个demo中,能够把机器人四周的颜色拖动到机器人的各个部 ...
- 使用构建工具gradle打包时,遇到的中文问题和解决方式
1.使用gradle clean war 命令将项目打成war包.这一过程gradle没有提示报错. 2.将得到的myapp.war复制到tomcat下webapps(部署war包) 3.启动to ...
- ROR部署到Heroku出现Application Error和code=H10 desc="App crashed“问题
1.问题发现之前的准备 在读<Learn Python In Hard Way>的时候,发现作者谈到一个非常有趣的事情,在做一些有趣的事情之前做的无聊的事情叫做yak shaving,牦牛 ...
- 多媒体开之之rtp 时间戳和负载类型介绍
(1)时间戳 (2)负载类型 (3)rtp 包头 (1)时间戳 有三个 一个实时间单位 timestamp_increse=(unsigned int)(90000.0 / framerate); / ...