洛谷P1853 投資的最大效益 動態規劃

題目
多重揹包,不過要多考慮一個年限,最開始用map複雜度太高,改用數組頂着優化勉強過了。
本金爲揹包空間,投資額爲消耗的空間,利息爲價值

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast", "inline")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <queue>
#include <map>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long ll;

struct Bond{
    int total;
    int excess;
};
int S, N, D;
Bond bond[11];
int dp[30000000];


int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> S >> N >> D;
    for (int i = 1; i <= D; i++) {
        cin >> bond[i].total >> bond[i].excess;
    }

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= D; j++) {
            for (int k = bond[j].total; k <= S; k++) {
                dp[k] = max(dp[k], dp[k - bond[j].total] + bond[j].excess);
            }
        }
        S += dp[S];
    }
    cout << S << endl;
    return 0;
}```

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章