HDU 2955_Robberies Thief Robbing the Bank [01 Backpack]

Question link: https://vjudge.net/contest/103424#problem/F

Reprinted in: https://blog.csdn.net/flynn_curry/article/details/50950787          The analysis of the problem-solving ideas is very good

 

Title:

First, several sets of data are given. The first line of each set of data is the total probability p of being caught (the total probability obtained at the end must be less than him, otherwise it will be caught), and then the number of banks that you want to rob. Then there are n lines, each line is the amount of money m[i] that the bank can grab and the probability p[i] of being caught, to find the maximum escape probability. The higher the probability of being caught, the lower the probability of escape.

 Problem solving ideas:

The first point is the most error-prone point. The total probability of being caught is regarded as the capacity of the backpack. There are two obvious mistakes. The requirement is 1-probability of being caught = probability of escape. The restriction condition and the maximum backpack capacity cannot be the same attribute. Furthermore, the probabilities are mostly floating-point numbers, which cannot be traversed with low precision. Therefore, the capacity of the backpack must be the amount of money, because the banks he can grab are limited, and the amount of money is also limited. Then it is to find the maximum escape probability, and each item in the question gives the probability of being caught, so it must be subtracted by 1 first. Also, the escape probability obtained at last decreases as the number of bank robberies increases. If one more bank is robbed, the amount of money will be converted into a product of probabilities, so the dynamic equation must also be changed. At the end of the traversal, the more money left, the less money you grab, and the greater the chance of escape. So traverse the backpack capacity from large to small, once it is greater than p, it is the maximum probability of jumping out.

 

#include <cstdio>  
#include <algorithm>   
#include <cmath>  
using namespace std;

const int N = 50005;

intmain ()
{
    int t, m0, m[N];
    double p0, p[N], ans[N];
    scanf("%d", &t);
    while (t--)
    {
        scanf("%lf%d", &p0, &m0);
        int sum = 0;
        for (int i = 0; i < m0; i++)
        {
            scanf("%d%lf", &m[i], &p[i]);
            sum += m[i];
        }
        memset(ans, 0, sizeof(ans));
        ans[0] = 1;
        for (int i = 0; i < m0; i++)
        {
            for (int j = sum; j >= m[i]; j--)
            {
                ans[j] = max(ans[j], ans[j - m[i]] * ( 1 - p[i]));            // ans[j] represents the maximum arrest probability with a total value of j 
            }
        }
        for (int i = sum; i >= 0; i--)
        {
            if (ans[i] >( 1 - p0))             // p0 is the probability of being arrested 
            {
                printf("%d\n", i);
                break;
            }
        }
    }
    return 0;
}

 

 

2018-04-30

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325129295&siteId=291194637