HDU 1203(I NEED A OFFER!)

0-1 backpack, knapsack capacity is the total amount of money, backpack value is the probability of obtaining the offer.

State transition equation of dp [j] = max (dp [j], 1 - (1 - dp [j - c [i]]) * (1 - p [i]))

#include <iostream>
#include <cstring>
#include <algorithm>
#include <iomanip>
using namespace std;
const int MAXN = 10005;

double dp[MAXN]; //动态数组,dp[i]表示钱数为i时,获得offer的最大概率
int c[MAXN]; //各学校的申请费用
double p[MAXN]; //各学校拿到offer的概率

int main()
{
    int n, m;
    while (cin >> n >> m)
    {
        if (n == 0 && m == 0)
            break;
        memset(dp, 0, sizeof(dp));
        memset(c, 0, sizeof(c));
        memset(p, 0, sizeof(p));

        for (int i = 1; i <= m; i++)
        {
            cin >> c[i] >> p[i];
        }
        for (int i = 1; i <= m; i++)
        {
            for (int j = n; j >= c[i]; j--)
                dp[j] = max(dp[j], 1 - (1 - dp[j - c[i]]) * (1 - p[i]));
        }
        cout << fixed << setprecision(1) << dp[n] * 100 << "%" << endl;
    }
    return 0;
}

Keep up.

Published 138 original articles · won praise 1 · views 7008

Guess you like

Origin blog.csdn.net/Intelligence1028/article/details/104613118