結果

問題 No.2869 yuusaan's Knapsacks
ユーザー rniyarniya
提出日時 2024-08-30 22:36:52
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 713 ms / 4,500 ms
コード長 2,712 bytes
コンパイル時間 3,183 ms
コンパイル使用メモリ 257,412 KB
実行使用メモリ 22,320 KB
最終ジャッジ日時 2024-08-30 22:37:03
合計ジャッジ時間 10,010 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 28 ms
10,956 KB
testcase_04 AC 28 ms
11,004 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 440 ms
22,192 KB
testcase_07 AC 81 ms
10,880 KB
testcase_08 AC 9 ms
6,944 KB
testcase_09 AC 9 ms
6,940 KB
testcase_10 AC 154 ms
14,096 KB
testcase_11 AC 21 ms
8,952 KB
testcase_12 AC 373 ms
19,240 KB
testcase_13 AC 17 ms
8,952 KB
testcase_14 AC 164 ms
12,164 KB
testcase_15 AC 47 ms
12,040 KB
testcase_16 AC 93 ms
13,068 KB
testcase_17 AC 713 ms
22,320 KB
testcase_18 AC 414 ms
18,212 KB
testcase_19 AC 271 ms
15,124 KB
testcase_20 AC 9 ms
6,944 KB
testcase_21 AC 277 ms
16,152 KB
testcase_22 AC 418 ms
16,152 KB
testcase_23 AC 18 ms
9,980 KB
testcase_24 AC 548 ms
19,232 KB
testcase_25 AC 13 ms
7,796 KB
testcase_26 AC 639 ms
16,152 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 95 ms
22,316 KB
testcase_29 AC 415 ms
22,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (auto& e : v) {
        is >> e;
    }
    return is;
}

template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    for (std::string_view sep = ""; const auto& e : v) {
        os << std::exchange(sep, " ") << e;
    }
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) {
    return y < x and (x = std::forward<U>(y), true);
}

template <class T, class U = T> bool chmax(T& x, U&& y) {
    return x < y and (x = std::forward<U>(y), true);
}

template <class T> void mkuni(std::vector<T>& v) {
    std::ranges::sort(v);
    auto result = std::ranges::unique(v);
    v.erase(result.begin(), result.end());
}

template <class T> int lwb(const std::vector<T>& v, const T& x) {
    return std::distance(v.begin(), std::ranges::lower_bound(v, x));
}

using ll = long long;

using namespace std;

const ll INF = 1e18;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, M;
    cin >> N >> M;
    vector<int> e(N), v(M), w(M);
    for (int i = 0; i < N; i++) cin >> e[i];
    for (int i = 0; i < M; i++) cin >> v[i] >> w[i];

    vector<ll> V(1 << M, 0), W(1 << M, 0);
    for (int S = 0; S < 1 << M; S++) {
        for (int i = 0; i < M; i++) {
            if (S >> i & 1) {
                V[S] += v[i];
                W[S] += w[i];
            }
        }
    }
    vector dp(N + 1, vector<ll>(1 << M, -INF)), pre(N + 1, vector<ll>(1 << M, -1));
    dp[0][0] = 0;

    for (int i = 0; i < N; i++) {
        for (int S = 0; S < 1 << M; S++) {
            ll& val = dp[i][S];
            if (val == -INF) continue;
            int rest = (1 << M) - 1 - S;
            for (int T = rest;; T = (T - 1) & rest) {
                if (W[T] > e[i]) continue;
                if (chmax(dp[i + 1][S | T], val + V[T])) pre[i + 1][S | T] = T;
                if (T == 0) break;
            }
        }
    }

    ll maxi = -INF, argmax = -1;
    for (int S = 0; S < 1 << M; S++) {
        if (chmax(maxi, dp[N][S])) {
            argmax = S;
        }
    }
    vector<vector<int>> res(N);
    for (int i = N - 1; i >= 0; i--) {
        int T = pre[i + 1][argmax];
        for (int j = 0; j < M; j++) {
            if (T >> j & 1) {
                res[i].emplace_back(j);
            }
        }
        argmax ^= T;
    }

    cout << maxi << "\n";
    for (int i = 0; i < N; i++) {
        cout << res[i].size();
        for (int& x : res[i]) cout << " " << x + 1;
        cout << "\n";
    }
    return 0;
}
0