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
*/

posted @ 2018-05-19 21:41  Roni_i  阅读(226)  评论(0编辑  收藏  举报