結果
問題 | No.2869 yuusaan's Knapsacks |
ユーザー | nono00 |
提出日時 | 2024-08-30 23:25:33 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 900 ms / 4,500 ms |
コード長 | 3,156 bytes |
コンパイル時間 | 3,014 ms |
コンパイル使用メモリ | 258,916 KB |
実行使用メモリ | 17,716 KB |
最終ジャッジ日時 | 2024-08-30 23:25:54 |
合計ジャッジ時間 | 10,917 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 31 ms
9,176 KB |
testcase_04 | AC | 31 ms
9,344 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 550 ms
17,716 KB |
testcase_07 | AC | 98 ms
9,216 KB |
testcase_08 | AC | 9 ms
6,940 KB |
testcase_09 | AC | 8 ms
6,940 KB |
testcase_10 | AC | 202 ms
11,532 KB |
testcase_11 | AC | 37 ms
7,540 KB |
testcase_12 | AC | 494 ms
15,392 KB |
testcase_13 | AC | 19 ms
7,560 KB |
testcase_14 | AC | 225 ms
9,992 KB |
testcase_15 | AC | 59 ms
9,992 KB |
testcase_16 | AC | 117 ms
10,764 KB |
testcase_17 | AC | 900 ms
17,712 KB |
testcase_18 | AC | 535 ms
14,544 KB |
testcase_19 | AC | 348 ms
12,312 KB |
testcase_20 | AC | 9 ms
6,944 KB |
testcase_21 | AC | 387 ms
12,956 KB |
testcase_22 | AC | 551 ms
13,080 KB |
testcase_23 | AC | 19 ms
8,320 KB |
testcase_24 | AC | 705 ms
15,432 KB |
testcase_25 | AC | 15 ms
6,944 KB |
testcase_26 | AC | 762 ms
13,208 KB |
testcase_27 | AC | 2 ms
6,944 KB |
testcase_28 | AC | 120 ms
17,712 KB |
testcase_29 | AC | 573 ms
17,708 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; template <class T> using max_heap = priority_queue<T>; template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>; #define rep(i, l, r) for (ll i = (l); i < (r); i++) #define rrep(i, r, l) for (ll i = (r); i-- > (l);) #define all(x) begin(x), end(x) template <class T> bool chmin(T& lhs, T rhs) { return lhs > rhs ? (lhs = rhs, true) : false; } template <class T> bool chmax(T& lhs, T rhs) { return lhs < rhs ? (lhs = rhs, true) : false; } struct IOIO { IOIO() { cin.tie(0)->sync_with_stdio(0); } } ioio; template <class S, class T> ostream& operator<<(ostream& os, const pair<S, T>& p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template <class T> ostream& operator<<(ostream& os, const vector<T>& vs) { os << '{'; rep(i, 0, (int)vs.size()) os << vs[i] << (i + 1 == (int)vs.size() ? "" : ", "); os << '}'; return os; } template <class T> ostream& operator<<(ostream& os, const set<T>& vs) { os << '{'; for (auto it = vs.begin(); it != vs.end(); it++) { if (it != vs.begin()) { os << ", "; } os << *it; } os << '}'; return os; } template <class T, class U> ostream& operator<<(ostream& os, const map<T, U>& vs) { os << '{'; for (auto it = vs.begin(); it != vs.end(); it++) { if (it != vs.begin()) { os << ", "; } os << *it; } os << '}'; return os; } #ifdef DEBUG void dump_func() { cerr << endl; } template <class Head, class... Tail> void dump_func(Head&& head, Tail&&... tail) { cerr << head; if (sizeof...(Tail) > 0) { cerr << ", "; } dump_func(std::move(tail)...); } #define dump(...) cerr << "[" + string(#__VA_ARGS__) + "] ", dump_func(__VA_ARGS__) #else #define dump(...) static_cast<int>(0) #endif int main() { int n, m; cin >> n >> m; vector<ll> a(n); rep(i, 0, n) cin >> a[i]; vector<ll> ws(m), vs(m); rep(i, 0, m) cin >> vs[i] >> ws[i]; vector<ll> sw(1 << m); vector<ll> sv(1 << m); rep(s, 0, 1 << m) rep(i, 0, m) if (s & (1 << i)) sw[s] += ws[i], sv[s] += vs[i]; const ll none = -1; vector dp(n + 1, vector<ll>(1 << m, none)); vector prv(n + 1, vector<int>(1 << m)); dp[0][0] = 0; rep(i, 0, n) { rep(s, 0, 1 << m) { if (dp[i][s] == none) continue; int tt = (~s) & ((1 << m) - 1); for (int t = tt;; t = (t - 1) & tt) { if (sw[t] <= a[i]) { if (chmax(dp[i + 1][s | t], dp[i][s] + sv[t])) { prv[i + 1][s | t] = t; } } if (t == 0) break; } } } ll max_v = -1; int max_s = 0; rep(i, 0, 1 << m) if (chmax(max_v, dp[n][i])) max_s = i; cout << max_v << endl; vector<int> ss; int j = max_s; rrep(i, n, 0) ss.push_back(prv[i + 1][j]), j = j ^ prv[i + 1][j]; reverse(all(ss)); rep(i, 0, n) { int s = ss[i]; cout << popcount((unsigned)s); rep(j, 0, m) if (s & (1 << j)) cout << ' ' << j + 1; cout << '\n'; } }