結果
問題 | No.2869 yuusaan's Knapsacks |
ユーザー | tnakao0123 |
提出日時 | 2024-09-04 15:03:23 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,086 ms / 4,500 ms |
コード長 | 2,003 bytes |
コンパイル時間 | 691 ms |
コンパイル使用メモリ | 55,488 KB |
実行使用メモリ | 12,476 KB |
最終ジャッジ日時 | 2024-09-04 15:03:33 |
合計ジャッジ時間 | 10,482 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 3 ms
7,372 KB |
testcase_02 | AC | 3 ms
9,308 KB |
testcase_03 | AC | 32 ms
11,112 KB |
testcase_04 | AC | 32 ms
10,852 KB |
testcase_05 | AC | 2 ms
7,248 KB |
testcase_06 | AC | 747 ms
12,404 KB |
testcase_07 | AC | 115 ms
8,008 KB |
testcase_08 | AC | 3 ms
6,940 KB |
testcase_09 | AC | 3 ms
7,920 KB |
testcase_10 | AC | 252 ms
10,788 KB |
testcase_11 | AC | 20 ms
8,124 KB |
testcase_12 | AC | 619 ms
11,728 KB |
testcase_13 | AC | 15 ms
6,948 KB |
testcase_14 | AC | 256 ms
9,760 KB |
testcase_15 | AC | 61 ms
10,280 KB |
testcase_16 | AC | 145 ms
10,148 KB |
testcase_17 | AC | 1,086 ms
12,476 KB |
testcase_18 | AC | 672 ms
12,052 KB |
testcase_19 | AC | 438 ms
11,768 KB |
testcase_20 | AC | 3 ms
8,008 KB |
testcase_21 | AC | 435 ms
10,792 KB |
testcase_22 | AC | 634 ms
10,944 KB |
testcase_23 | AC | 14 ms
8,104 KB |
testcase_24 | AC | 854 ms
11,540 KB |
testcase_25 | AC | 10 ms
6,944 KB |
testcase_26 | AC | 894 ms
11,440 KB |
testcase_27 | AC | 2 ms
6,944 KB |
testcase_28 | AC | 141 ms
12,392 KB |
testcase_29 | AC | 702 ms
12,292 KB |
ソースコード
/* -*- coding: utf-8 -*- * * 2869.cc: No.2869 yuusaan's Knapsacks - yukicoder */ #include<cstdio> #include<vector> #include<algorithm> using namespace std; /* constant */ const int MAX_N = 16; const int MAX_M = 16; const int MBITS = 1 << MAX_M; /* typedef */ using ll = long long; using vi = vector<int>; /* global variables */ int es[MAX_N], vs[MAX_M], ws[MAX_M]; int vss[MBITS], dp[MAX_N + 1][MBITS], ps[MAX_N + 1][MBITS]; ll wss[MBITS]; vi qs[MAX_N]; /* subroutines */ /* main */ int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", es + i); for (int i = 0; i < m; i++) scanf("%d%d", vs + i, ws + i); int mbits = 1 << m, mmsk = mbits - 1; for (int bits = 1, msb = 1, msi = 0; bits < mbits; bits++) { if ((msb << 1) <= bits) msb <<= 1, msi++; vss[bits] = vss[bits ^ msb] + vs[msi]; wss[bits] = wss[bits ^ msb] + ws[msi]; } fill(dp[0], dp[0] + mbits, -1); dp[0][0] = 0, ps[0][0] = -1; for (int i = 0; i < n; i++) { copy(dp[i], dp[i] + mbits, dp[i + 1]); for (int bits = 0; bits < mbits; bits++) ps[i + 1][bits] = bits; for (int bits = 0; bits < mbits; bits++) if (dp[i][bits] >= 0) { int msk1 = bits ^ mmsk; for (int bits1 = msk1;;) { int d = dp[i][bits] + vss[bits1]; if (es[i] >= wss[bits1] && dp[i + 1][bits | bits1] < d) { dp[i + 1][bits | bits1] = d; ps[i + 1][bits | bits1] = bits; } if (bits1 == 0) break; bits1 = (bits1 - 1) & msk1; } } } int maxv = -1, maxb = -1; for (int bits = 0; bits < mbits; bits++) if (maxv < dp[n][bits]) maxv = dp[n][bits], maxb = bits; for (int i = n, bits = maxb; i > 0; bits = ps[i][bits], i--) { int bs = bits ^ ps[i][bits]; for (int j = 0, bj = 1; j < m; j++, bj <<= 1) if (bs & bj) qs[i - 1].push_back(j); } printf("%d\n", maxv); for (int i = 0; i < n; i++) { printf("%d", (int)qs[i].size()); for (auto u: qs[i]) printf(" %d", u + 1); putchar('\n'); } return 0; }