結果

問題 No.2741 Balanced Choice
ユーザー ponjuiceponjuice
提出日時 2024-04-17 23:40:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,109 bytes
コンパイル時間 2,622 ms
コンパイル使用メモリ 200,896 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-17 23:40:09
合計ジャッジ時間 3,636 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
6,812 KB
testcase_01 AC 84 ms
6,940 KB
testcase_02 AC 83 ms
6,944 KB
testcase_03 AC 84 ms
6,940 KB
testcase_04 AC 88 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 50 ms
6,944 KB
testcase_08 AC 71 ms
6,940 KB
testcase_09 AC 48 ms
6,944 KB
testcase_10 AC 55 ms
6,940 KB
testcase_11 AC 73 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<int> sol(vector<pair<int, int>>& wv, const int MAX_W) {
    vector<int> dp(MAX_W + 1, -1e9);
    dp[0] = 0;

    for (int i = 0; i < wv.size(); i++) {
        vector<int> ndp(MAX_W + 1, -1e9);
        auto [w, v] = wv[i];

        for (int i = 0; i <= MAX_W; i++) {
            ndp[i] = max(ndp[i], dp[i]);
            if(i + w <= MAX_W) ndp[i + w] = max(ndp[i + w], dp[i] + v);
        }

        dp.swap(ndp);
    }

    return dp;
}

int main() {
    int n, max_w, d;
    cin >> n >> max_w >> d;

    vector<pair<int, int>> t0, t1;
    for (int i = 0; i < n; i++) {
        int t, w, v;
        cin >> t >> w >> v;
        if (t == 0) t0.emplace_back(w, v);
        if (t == 1) t1.emplace_back(w, v);
    }

    vector<int> dp0 = sol(t0, max_w);
    vector<int> dp1 = sol(t1, max_w);


    int ans = 0;
    for (int i = 0; i <= max_w; i++) {
        for (int j = 0; j <= max_w; j++) {
            if (i + j > max_w) break;
            if (abs(i - j) > d) continue;
            ans = max(ans, dp0[i] + dp1[j]);
        }
    }
    cout << ans << endl;
}
0