HDU Problem 2955 Robberies 【01背包】

Robberies

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

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:  2159 2844 1171 1864 2191 
 
首先的容量本应该是上限的概率,但是浮点型数据不能当数组下标,况且还要相乘。但是转化思路,可以让不平安概率的乘积最大,数组下标当做最大的金额,找出满足题意的最大下标就可以了。
其次,对于第二层循环 for (int j = sum; j >= val[i]; j++)  dp[ j ]  =  max( dp[ j ],  dp[ j - val[i] ] * P[i]);  , 每次进行j--运算,会把所有的val数组给填满的,而我们所要的是答案只会是作为下标、题目中给出的几个数的组合,要是选了一个不该选的咋办,抢一个银行只拿一部分钱吗? 。。。。肿么办?
我看了好多人的博客,无视这个问题,通过仔细观察递归过程发现,除了dp[0] = 1, 其他的值都被赋值0,所以在进行转化的时候不是val组合的都因为相乘变成了0。
#include <cmath>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
//typedef __int64 Int;
typedef long long LL;
const double ESP = 1e-5;
const double Pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int INF = 0x3f3f3f3f;
double dp[MAXN], P[MAXN];
int val[MAXN];
void init(int x) {
    for (int i = 0; i <= x; i++) {
        dp[i] = 0;
    }
}
int main() {
    int t, n;double lim;
    scanf("%d", &t);
    while (t--) {
        scanf("%lf%d", &lim, &n);
        int sum = 0;
        for (int i = 0; i < n; i++) {
            scanf("%d%lf", &val[i], &P[i]);
            P[i] = 1 - P[i]; sum += val[i];
        }
        init(sum); dp[0] = 1;
        for (int i = 0; i < n; i++) {
            for (int j = sum; j >= val[i]; j--) {
                dp[j] = max(dp[j], dp[j - val[i]]*P[i]);
            }
        }
        for (int i = sum; i >= 0; i--) {
            if (dp[i] >= 1 - lim) {
                printf("%d
", i);
                break;
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/cniwoq/p/6770825.html