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 ...
随机推荐
- C++输出中文字符(转)
C++输出中文字符 1. cout 场景1: 在源文件中定义 const char* str = "中文" 在 VC++ 编译器上,由于Windows环境用 GBK编码,所以字符串 ...
- hive支持sql大全
转自:http://www.aboutyun.com/thread-7316-1-1.html 一.关系运算:1. 等值比较: = 语法:A=B 操作类型:所有基本类型 描述: 如果表达式A与表达式B ...
- Java 动态代理(转)
一.代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后 处理消息等.代理类与委托类之间通常会存在 ...
- FileNameExtensionFilter文件过滤
package com.soft.test; import javax.swing.*; import javax.swing.filechooser.*; import java.awt.event ...
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- android 中获取网络状态、判断3G、2G、wifi网络、判断wifi是否打开、获取本机地址、获取本机串号IMEI整理
代码如下:package com.android.xym; import java.io.IOException; import java.net.HttpURLConnection; import ...
- Delphi的WebBrowser改造,对网页中Alter等对话框的改造方法(通过COM来改造)
刚有一段时间没做博客了,今天刚好有人问了这个问题,而自己以前也弄过,于是这里有了一篇新的博文. 关于改造WebBrowser控件的一些技巧,大家可以参考MSDN或者蒋晟写的一个东西,里面有讲的很详细的 ...
- ProFTPD 初探
ProFTPD:一个Unix平台上或是类Unix平台上(如Linux, FreeBSD等)的FTP服务器程序.
- BZOJ 无数据题集合
题目 http://www.lydsy.com/JudgeOnline/problem.php?id=1142 http://www.lydsy.com/JudgeOnline/problem.php ...
- poj 2228 Naptime dp
这个题目的状态还是比较好想的,dp[i][j]表示已经睡了i个时段,最后睡在j时段的最优值,但是需要处理环的情况,我的做法是算两次,第一次不处理环,第二次强制性要求第一个时段需要睡,然后查看dp[m] ...