首页 > ACM题库 > HDU-杭电 > HDU 2955-Robberies-背包问题-[解题报告]HOJ
2014
02-24

HDU 2955-Robberies-背包问题-[解题报告]HOJ

Robberies

问题描述 :

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.

输入:

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 .

输出:

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 .

样例输入:

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

样例输出:

2
4
6

#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <iostream>

using namespace std;
double dp[10005];
double w[105];
int v[105];
int main()
{
    int n,t;
    double p;
    cin>>t;
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        int sum=0;
        cin>>p>>n;
         p=1.0-p;
        for(int i=0;i<n;i++)
        {
            cin>>v[i]>>w[i];
            w[i]=1.0-w[i];
            sum+=v[i];
        }
        dp[0]=1.0;
        for(int i=0;i<n;i++)
        for(int j=sum;j>=v[i];j--)
        dp[j]=max(dp[j],dp[j-v[i]]*w[i]);

        for(int i=sum;i>=0;i--)
        if(dp[i]>p)
        {
            cout<<i<<endl;
            break;
        }
    }
    return 0;
}

解题参考:http://blog.csdn.net/weyuli/article/details/9255695


  1. 代码是给出了,但是解析的也太不清晰了吧!如 13 abejkcfghid jkebfghicda
    第一步拆分为 三部分 (bejk, cfghi, d) * C(13,3),为什么要这样拆分,原则是什么?