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 Mjmillions, 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 |
Note
For the first case, if he wants to rob bank 1 and 2, then the probability of getting caught is 0.02 + (1 - 0.02) * .03 = 0.0494which is greater than the given probability (0.04). That's why he has only option, just to rob rank 2.
题解:给你一个总概率p,再给你N个银行,里面有钱vi,被抓到的概率是pi,要使被抓住的概率小于p,也就是没被抓到的概率大于1-p就好了,总钱数当作背包的容量,概率当作背包的值;
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define P_ printf(" ")
#define T_T while(T--)
const int MAXN=110;
struct Node{
double p;
int w;
friend bool operator < (Node a,Node b){
if(a.p<b.p)return true;
else return false;
}
};
double bag[10010];
Node dt[MAXN];
int main(){
int T,kase=0;
SI(T);
T_T{
double p;
int N;
scanf("%lf%d",&p,&N);
int sum=0;
for(int i=0;i<N;i++)scanf("%d%lf",&dt[i].w,&dt[i].p),sum+=dt[i].w;
sort(dt,dt+N);
mem(bag,0);bag[0]=1;
for(int i=0;i<N;i++){
for(int j=sum;j>=dt[i].w;j--)
bag[j]=max(bag[j],bag[j-dt[i].w]*(1-dt[i].p));
}
for(int i=sum;i>=0;i--){
if(bag[i]>1-p){
printf("Case %d: %d\n",++kase,i);break;
}
}
}
return 0;
}
Just another Robbery(背包)的更多相关文章
- (概率 01背包) Just another Robbery -- LightOJ -- 1079
http://lightoj.com/volume_showproblem.php?problem=1079 Just another Robbery As Harry Potter series i ...
- LightOJ - 1079 Just another Robbery —— 概率、背包
题目链接:https://vjudge.net/problem/LightOJ-1079 1079 - Just another Robbery PDF (English) Statistics ...
- 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(概率, 背包)
链接: https://vjudge.net/problem/LightOJ-1079#author=feng990608 题意: As Harry Potter series is over, Ha ...
- LightOJ 1079 Just another Robbery (01背包)
题目链接 题意:Harry Potter要去抢银行(wtf???),有n个银行,对于每个银行,抢的话,能抢到Mi单位的钱,并有pi的概率被抓到.在各个银行被抓到是独立事件.总的被抓到的概率不能超过P. ...
- Hdu 2955 Robberies 0/1背包
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU2955 背包DP
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 2955 01背包(思维)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- java 中有几种方法可以实现一个线程? 用什么关键字修 饰同步方法? stop()和 suspend()方法为何不推荐使用?
java5 以前, 有如下两种:第一种:new Thread(){}.start();这表示调用 Thread 子类对象的 run 方法, new Thread(){}表示一个Thread 的匿名子类 ...
- realloc,c语言
realloc #include <stdlib.h> main() { char* ptr=NULL; char* ptr2=NULL; ptr = malloc(); printf(& ...
- trangle
#include<iostream> #include<algorithm> using namespace std; int main() { int a,b,c; whil ...
- Java基础知识总结(二)
&和&&的区别: 按位与:a&b是把a和b都转换成二进制数后逐位进行与的运算.若两数字的某位都为1,则该位的运算结果才为1.运算的最终结果是数字. 逻辑与:a& ...
- font awesome使用笔记
背景 今天将BS项目部署到IIS服务器上时.首次打开一个使用font awesome图标集的页面是加载非常慢. 于是果断按下F12查看具体页面的请求时常.除去其他异步数据的加载消耗时间以外.我居然看到 ...
- watch命令详解(linux)
watch命令详解(linux) 在维护系统时经常需要实时查看系统的运行情况,比如实时的系统连接数之类的.在linux可以通过watch命令,实时监控每一条命令执行的结果动态变化. ...
- codeforces 463C Gargari and Bishops
题目链接 这个题, 最主要的应该是找到对角线上的格子的关系. “ \" 这种对角线, 关系是x-y+n相等, ” / “ 这种, 关系是x+y相等.知道每个格子的两种对角线的值, 那么这个格 ...
- 【转】linux Centos 6.5 安装桌面环境GNOME
在某种场合之下,我们使用的Linux还是要选择安装桌面环境的,所以在这里介绍一下如何给没有安装桌面环境的系统安装桌面环境. 以Centos 6.5 为例演示一下如何安装桌面环境. 一.首先查看系统的运 ...
- python总结
环境:django,numpy,matplotlib, 解释语言:开发效率高,通用性强,内置方便的数据容器,易于扩展和嵌入. 语言:lua--嵌入式/网络/APP,erlang--嵌入式,python ...
- hdoj 2546 饭卡(0-1背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 思路分析:该问题为0-1背包问题的变形题:问题求余额最少,设开始的余额为V,则求得用V-5可以买 ...