結果
問題 | No.2026 Yet Another Knapsack Problem |
ユーザー | suisen |
提出日時 | 2022-04-22 14:36:08 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 3,183 ms / 10,000 ms |
コード長 | 2,610 bytes |
コンパイル時間 | 2,199 ms |
コンパイル使用メモリ | 79,784 KB |
実行使用メモリ | 129,736 KB |
最終ジャッジ日時 | 2024-07-19 12:47:56 |
合計ジャッジ時間 | 31,634 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 99 ms
40,256 KB |
testcase_01 | AC | 117 ms
41,248 KB |
testcase_02 | AC | 100 ms
39,984 KB |
testcase_03 | AC | 138 ms
43,360 KB |
testcase_04 | AC | 126 ms
42,688 KB |
testcase_05 | AC | 115 ms
41,396 KB |
testcase_06 | AC | 142 ms
43,672 KB |
testcase_07 | AC | 134 ms
42,780 KB |
testcase_08 | AC | 114 ms
40,920 KB |
testcase_09 | AC | 159 ms
43,804 KB |
testcase_10 | AC | 138 ms
42,824 KB |
testcase_11 | AC | 163 ms
43,516 KB |
testcase_12 | AC | 114 ms
41,388 KB |
testcase_13 | AC | 154 ms
43,948 KB |
testcase_14 | AC | 129 ms
42,776 KB |
testcase_15 | AC | 114 ms
41,124 KB |
testcase_16 | AC | 139 ms
42,800 KB |
testcase_17 | AC | 132 ms
42,004 KB |
testcase_18 | AC | 114 ms
41,208 KB |
testcase_19 | AC | 131 ms
42,448 KB |
testcase_20 | AC | 119 ms
41,240 KB |
testcase_21 | AC | 130 ms
42,380 KB |
testcase_22 | AC | 127 ms
41,840 KB |
testcase_23 | AC | 140 ms
42,952 KB |
testcase_24 | AC | 116 ms
41,112 KB |
testcase_25 | AC | 126 ms
41,440 KB |
testcase_26 | AC | 141 ms
43,100 KB |
testcase_27 | AC | 126 ms
41,788 KB |
testcase_28 | AC | 305 ms
48,340 KB |
testcase_29 | AC | 333 ms
48,880 KB |
testcase_30 | AC | 336 ms
48,700 KB |
testcase_31 | AC | 301 ms
48,704 KB |
testcase_32 | AC | 310 ms
48,332 KB |
testcase_33 | AC | 328 ms
49,000 KB |
testcase_34 | AC | 294 ms
48,552 KB |
testcase_35 | AC | 264 ms
48,180 KB |
testcase_36 | AC | 317 ms
48,616 KB |
testcase_37 | AC | 313 ms
48,812 KB |
testcase_38 | AC | 3,151 ms
129,168 KB |
testcase_39 | AC | 2,923 ms
129,696 KB |
testcase_40 | AC | 2,999 ms
129,088 KB |
testcase_41 | AC | 3,119 ms
129,148 KB |
testcase_42 | AC | 3,061 ms
129,736 KB |
testcase_43 | AC | 3,167 ms
129,140 KB |
testcase_44 | AC | 3,183 ms
129,056 KB |
ソースコード
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { var sc = new Scanner(System.in); var pw = new PrintWriter(System.out, false); solve(sc, pw); sc.close(); pw.flush(); pw.close(); } public static final long INF = 1L << 60; public static void solve(Scanner sc, PrintWriter pw) { final int n = Integer.parseInt(sc.next()); var c = new int[n + 1]; var v = new long[n + 1]; for (int i = 1; i <= n; i++) { c[i] = Integer.parseInt(sc.next()); v[i] = Integer.parseInt(sc.next()); } var dp = new long[n + 1][n + 1]; for (int i = 0; i <= n; i++) Arrays.fill(dp[i], -INF); dp[0][0] = 0; for (int i = n; i > 0; i--) { final int max_num = n / i; final int max_sum = n; for (int num = 0; num <= max_num; num++) { final int sum_r = num > 0 ? i - 1 : max_sum; for (int sum = 0; sum <= sum_r; sum++) { final int max_p = Math.min(max_num - num, (max_sum - sum) / i); var init = new long[max_p + 1]; for (int p = 0; p <= max_p; ++p) { init[p] = dp[num + p][sum + i * p] - (num + p) * v[i]; } var seg = new RangeMaxSegmentTree(init); for (int p = 0; p <= max_p; ++p) { dp[num + p][sum + i * p] = seg.max(Math.max(0, p - c[i]), p + 1) + (num + p) * v[i]; } } } } for (int i = 1; i <= n; i++) { pw.println(Arrays.stream(dp[i]).max().getAsLong()); } } public static class RangeMaxSegmentTree { private int _n; private long[] _dat; public RangeMaxSegmentTree(long[] dat) { _n = dat.length; _dat = new long[2 * _n]; System.arraycopy(dat, 0, _dat, _n, _n); for (int i = _n - 1; i > 0; i--) { _dat[i] = Math.max(_dat[2 * i], _dat[2 * i + 1]); } } public long max(int l, int r) { long res = Long.MIN_VALUE; l += _n; r += _n; while (l < r) { if ((l & 1) != 0) res = Math.max(res, _dat[l++]); if ((r & 1) != 0) res = Math.max(res, _dat[--r]); l >>= 1; r >>= 1; } return res; } } }