HDU 2955 【01背包/小数/概率DP】
Robberies
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 29499    Accepted Submission(s): 10797
Problem Description
The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable job at a university.
For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.
His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.
Input
The first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj and a floating point number Pj .
Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .
Output
For each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set.
Notes and Constraints
0 < T <= 100
0.0 <= P <= 1.0
0 < N <= 100
0 < Mj <= 100
0.0 <= Pj <= 1.0
A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.
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
Sample Output
2
4
6
Source
IDI Open 2009
【题意】:先是给出几组数据,每组数据第一行是总被抓概率p(最后求得的总概率必须小于他,否则被抓),然后是想抢的银行数n。然后n行,每行分别是该银行能抢的钱数c[i]和被抓的概率p[i],求最大逃跑概率。被抓的概率越大,逃跑概率越小。
【分析】:这个背包建模,把概率当价值, 偷钱价值之和当体积。01背包求dp[i]表示获得i的钱不被抓的最大概率。题目中给定价值和被抓概率,但是被抓概率不可以用乘积来组合计算,举个例子,比如第一个银行3%被抓几率,第二个5%被抓几率,那么乘起来会变成0.15%,抢的越多,被抓概率却越小了!显然不对。因此要转换成不被抓几率,上述例子则变为第一家97%不被抓,第二家95%不被抓,乘起来就是92.15%,抢的越多,不被抓的几率越来越小!即被抓几率越来越大,这样才是符合常理的。
那么背包体积应该是什么呢?先看最普通01背包,用数个cost来填充V,使得value之和尽量大,那么这题就应该是用数个money填充总money,使得不被抓几率尽量大。那转移方程就是dp[j]=max(dp[j],dp[j-w]*c),这里和01背包的区别就是从+改成了*。 然后得到dp数组是0~V情况下的逃跑概率的最优(大)值,这根答案有什么关系?逆序枚举每一种情况,若此情况下的dp值即不被抓几率大于等于题目中所给的不被抓几率,那就输出,**逆序着从大到小枚举**保证了找到的一个解是最优解。
【总结】:
1:在有AB两个银行时,抢某个银行被抓的概率的计算假设A为pA,B为pB。则p被抓=pA+(1-pA)pB=pB+(1-pB)pA,因此这个关系是比较复杂的,可以做一个转化,不被抓的概率
则p不被抓=(1-pA)*(1-pB) 故对输入全部用1减下!
2:就是这题目要对钱当做体积,求一个当前钱数的不被抓概率。之后就是01背包的过程不再做加法而是乘法了。
3:初始状态dp[0]=1;显然的不抢是绝对安全的!
【代码】:
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int MAXN =  1e3 + 5;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int m0,c[maxm],k;
double p0,p[maxm],dp[maxm];
int main() //dp[i]表示获得i的钱不被抓的最大概率
{
    int t;
    cin>>t;
    while(t--)
    {
        ms(dp,0);
        int sum=0;
        cin>>p0>>m0;
        for(int i=0; i<m0; i++){
            cin>>c[i]>>p[i];
            sum+=c[i];
        }
        dp[0]=1;      //要注意初始化,dp[0]即获得0元(不抢劫),那么被抓的概率为0,逃跑的概率为1
        for(int i=0;i<m0;i++){
            for(int j=sum; j>=c[i]; j--){
                dp[j] = max(dp[j], dp[j-c[i]] * (1-p[i]) );
            }
        }
        for(int i=sum;i>=0;i--){
            if(dp[i]>(1-p0)){
                cout<<i<<endl; break;
            }
        }
    }
    return 0;
}
/*
1
8 2
2 100 4
4 100 2
400
*/
												
											HDU 2955 【01背包/小数/概率DP】的更多相关文章
- HDU 1203 【01背包/小数/概率DP】
		
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
 - hdu 2955 01背包
		
http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...
 - HDU 2955 【01背包+小数概率】
		
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
 - HDU 2955 01背包(思维)
		
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
 - Robberies   hdu 2955   01背包
		
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
 - hdu 1203  01背包  I need a offer
		
hdu 1203 01背包 I need a offer 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 题目大意:给你每个学校得到offe ...
 - HDU 2955  Robberies  背包概率DP
		
A - Robberies Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
 - HDU 1203 I NEED A OFFER!(01背包+简单概率知识)
		
I NEED A OFFER! Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
 - HDU 1203 I NEED A OFFER! (动态规划、01背包、概率)
		
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
 
随机推荐
- Lazarus教程 中文版后续给出
			
市面上有介绍Delphi的书籍(近来Delphi的书也是越来越少了),但没有一本系统的介绍Lazarus的书,这本书是网上的仅有的一本Lazarus教程,目前全部是英文,不过我已经着手开始翻译,争取尽 ...
 - Django笔记 —— 模板
			
最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...
 - node + express + iis + iisnode + urlrewrite搭建站点
			
前提条件:安装iis的电脑 准备条件: 1.下载iisnode 地址https://github.com/tjanczuk/iisnode/wiki/iisnode-releases 安装 2.下载 ...
 - nyoj 题目44 子串和
			
子串和 时间限制:5000 ms | 内存限制:65535 KB 难度:3 描述 给定一整型数列{a1,a2...,an},找出连续非空子串{ax,ax+1,...,ay},使得该子序列的和最 ...
 - 【Luogu】P3760异或和(权值树状数组)
			
题目链接 再次声明以后我见到位运算一定第一时间想把它拆成每一位算 本题就是有个前缀和sum[],然后让你求每一位有多少对i,j满足sum[i]-sum[j]在那一位上是1 考虑怎样才能减出1来 如果s ...
 - hdu 1195 Open the Lock (BFS)
			
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
 - [洛谷P2216][HAOI2007]理想的正方形
			
题目大意:有一个$a\times b$的矩阵,求一个$n\times n$的矩阵,使该区域中的极差最小. 题解:二维$ST$表,每一个点试一下是不是左上角就行了 卡点:1.用了一份考试时候写的二维$S ...
 - @Transactional(rollbackFor=Exception.class)的作用
			
在项目中,@Transactional(rollbackFor=Exception.class),如果类加了这个注解,那么这个类里面的方 法抛出异常,就会回滚,数据库里面的数据也会回滚. 这种设置是因 ...
 - linux命令Netstat
			
1.需求 了解Netstat命令 2.简介 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multi ...
 - bzoj1185 [HNOI2007]最小矩形覆盖 旋转卡壳求凸包
			
[HNOI2007]最小矩形覆盖 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 2081 Solved: 920 ...