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 ...
随机推荐
- IntelliJ IDEA创建文件时自动填入作者时间 定制格式
IntelliJ IDEA创建文件时自动填入作者时间 定制格式 学习了:https://blog.csdn.net/Hi_Boy_/article/details/78205483 学习了:http: ...
- 安装配置 Kafka Manager 分布式管理工具
Kafka Manager 特性,它支持以下内容(官方译解): 管理多个群集容易检查集群状态(主题,消费者,偏移量,经纪人,副本分发,分区分配)运行首选副本选举使用选项生成分区分配,以选择要使用的代理 ...
- 提升Web性能的技巧
1. 采用反向代理服务器(Reverse Proxy Server)来对应用进行加速和保护 其作用主要在以下三方面: 负载平衡 – 运行在反向代理服务器上的负载平衡器会在不同的不服务器 间进行传输 ...
- 摄像机Rtsp地址格式大全
各厂家rtsp地址格式如下: 一. 海康.中威摄像机 格式1 主码流:rtsp://admin:12345@192.168.1.64:554/Streaming/Channels/1 子码流:rts ...
- Java性能小技巧
局部决定总体. 一个应用的总体性能取决于每一个组件的性能. 以下是一些帮助你提高应用性能的Java编程技巧: 编程技巧 原因及策略 避免反复创建对象 为什么: 更少的对象会须要更少的垃圾回收 使用的空 ...
- Java 8 Collectors to Map
1. 介绍 在本教程中,我们将讨论Collectors类的toMap()方法.我们使用它将流收集到一个Map实例中. 对于本教程中涉及的所有示例,我们将使用图书列表作为数据源,并将其转换为不同的Map ...
- Intellij IDEA如何不显示参数提示
刚安装了IDEA之后,调用方法的时候会提示方法中的参数,就像下面这样: 虽然IDEA也是好心,提示,但是劳资看着难受啊. 如果觉得不习惯,不想看参数名,可以用下图的方式取消.具体是: setting ...
- cacti 安装和组件添加
安装cacti 步骤 1.准备lamp环境 2.准备所需包:rrdtool(绘图) cacti(安装程序) net-snmpd(数据收集) 3.安装所需库文件 rrdtool所需库文件有: cairo ...
- Unity3D研究院之Inspector视图中的get/set使用
get set 使用起来很方便,但是编辑时在Inspector视图中问题就来了,因为get/set的属性即使是public了,但是在Inspector视图中依然不显示..谷歌一下估计就是下面这样的答案 ...
- excel十几万行数据快速导入数据库研究(转,下面那个方法看看还是可以的)
先贴原来的导入数据代码: 8 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "www.setting ...