結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー suisensuisen
提出日時 2022-04-25 17:25:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,676 bytes
コンパイル時間 1,273 ms
コンパイル使用メモリ 94,732 KB
実行使用メモリ 5,432 KB
最終ジャッジ日時 2023-09-09 20:18:03
合計ジャッジ時間 14,099 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,504 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,380 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 5 ms
4,872 KB
testcase_33 AC 5 ms
5,432 KB
testcase_34 AC 20 ms
5,100 KB
testcase_35 WA -
testcase_36 AC 10 ms
5,120 KB
testcase_37 WA -
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// TLE Θ(N^3) 後ろからやって状態数削減、遷移は高速化せず
// + 配るDPで-∞からの遷移を枝刈り
// + 増加部分以外を捨てる嘘高速化 

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <chrono>
#include <iostream>
#include <limits>
#include <vector>

constexpr int64_t inf = std::numeric_limits<int64_t>::max() / 2;

int main() {
    int n;
    std::cin >> n;

    std::vector<int32_t> c(n + 1);
    std::vector<int64_t> v(n + 1);

    int64_t cumulative_max = -inf;
    for (int i = 1; i <= n; ++i) {
        std::cin >> c[i] >> v[i];
        if (cumulative_max > v[i]) {
            c[i] = 0;
        } else {
            cumulative_max = v[i];
        }
    }

    std::vector dp(n + 1, std::vector<int64_t>(n + 1, -inf));
    dp[0][0] = 0;

    for (uint32_t i = n; i > 0; --i) {
        if (c[i] == 0) continue;
        const uint32_t max_num = uint32_t(n) / i;
        // dp[num][sum] = max{ pd[num-p][sum-p*i]+p*v[i] | 0<=p<=c[i] } ⋃ {-∞}
        for (uint32_t num = max_num; num --> 0;) {
            for (uint32_t sum = n - i + 1; sum --> num * i;) {
                if (dp[num][sum] == -inf) continue;
                const uint32_t pmax = std::min({ max_num - num, (n - sum) / i, uint32_t(c[i]) });
                for (uint32_t p = 1; p <= pmax; ++p) {
                    dp[num + p][sum + i * p] = std::max(dp[num + p][sum + i * p], dp[num][sum] + p * v[i]);
                }
            }
        }
    }

    for (int32_t k = 1; k <= n; ++k) {
        std::cout << *std::max_element(dp[k].begin(), dp[k].end()) << '\n';
    }
    return 0;
}
0