HDU 2955 Robberies(01背包变形)

Robberies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16522    Accepted Submission(s): 6065


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
 

 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  1203 2159 2844 1171 1864
 
这题是01背包的灵活变形,需要懂一点概率知识,就是独立事件的计算公式,是相乘。
样例很具有迷惑性,让你觉得只要把概率扩大100倍,然后硬套01背包就可以了,其实不是这样,数据会有很多位小数,而且同时抢几家银行被抓的概率也并不是几个概率相加所以如果拿概率当容量,最大抢钱数当价值,列出f[j]=max(f[j], f[j-p[i]]+m[i])肯定是错的。
 
所以就要转换思路,把概率当成价值,抢钱数当成容量。然后就用两种做法,1) f[i]表示抢劫i钱的最大不被抓概率, 2)f[i]表示抢劫i钱的最小被抓概率  两种最后都要通过一个循环判断出零界为止的那个i的值,就是最终答案。
 
1)
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345
#define M 12

float f[N],p[N],P;
int m[N],n;

int main()
{
//    freopen("1.txt", "r", stdin);
//    freopen("2.txt", "w", stdout);
    int T;cin>>T;
    while(T--)
    {
        int sum=0;
        scanf("%f%d",&P,&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%f",&m[i],&p[i]);
            sum+=m[i];
        }
        memset(f,0,sizeof(f));//初始化
        f[0]=1.0;//边界,抢劫0钱不被抓的概率是1

        for(int i=1;i<=n;i++)
        {
            for(int j=sum;j>=m[i];j--)
            {
                f[j]=max(f[j], f[j-m[i]]*(1-p[i]) );//独立事件相乘
            }
        }

        for(int i=sum;i>=0;i--)
        {
//            printf("%f ",f[i]);
            if(f[i]>=(1-P))//找到第一不被抓概率大于等于(1-P)的i就是答案
            {
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}

 

2)

#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345

float f[N],p[N],P;
int m[N],n;

int main()
{
    int T;cin>>T;
    while(T--)
    {
        int sum=0;
        scanf("%f%d",&P,&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%f",&m[i],&p[i]);
            sum+=m[i];
        }

        for(int i=0;i<N-5;i++)//初始化
            f[i]=1.0;
        f[0]=0.0;//边界,抢劫0钱的被抓概率是0

        for(int i=1;i<=n;i++)
        {
            for(int j=sum;j>=m[i];j--)
            {
                f[j]=min(f[j], 1-(1-f[j-m[i]])*(1-p[i]) );//概率知识,独立事件,对立事件
            }
        }

        for(int i=sum;i>=0;i--)
        {
//            printf("%f ",f[i]);
            if(f[i]<=P)//找到第一个概率小于等于安全概率P的i,就是答案
            {
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}

 

 

 

posted @ 2015-08-23 07:52  wmxl  阅读(204)  评论(0编辑  收藏  举报