結果
問題 | No.2026 Yet Another Knapsack Problem |
ユーザー | suisen |
提出日時 | 2022-04-22 14:27:55 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,240 ms / 10,000 ms |
コード長 | 2,640 bytes |
コンパイル時間 | 2,010 ms |
コンパイル使用メモリ | 79,212 KB |
実行使用メモリ | 95,732 KB |
最終ジャッジ日時 | 2024-07-19 12:34:59 |
合計ジャッジ時間 | 16,861 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 111 ms
41,248 KB |
testcase_01 | AC | 98 ms
40,048 KB |
testcase_02 | AC | 115 ms
41,544 KB |
testcase_03 | AC | 128 ms
41,384 KB |
testcase_04 | AC | 128 ms
41,872 KB |
testcase_05 | AC | 121 ms
41,688 KB |
testcase_06 | AC | 125 ms
41,212 KB |
testcase_07 | AC | 120 ms
41,216 KB |
testcase_08 | AC | 112 ms
41,128 KB |
testcase_09 | AC | 130 ms
41,468 KB |
testcase_10 | AC | 130 ms
41,860 KB |
testcase_11 | AC | 137 ms
41,764 KB |
testcase_12 | AC | 104 ms
41,136 KB |
testcase_13 | AC | 137 ms
41,388 KB |
testcase_14 | AC | 128 ms
41,872 KB |
testcase_15 | AC | 115 ms
41,280 KB |
testcase_16 | AC | 134 ms
41,340 KB |
testcase_17 | AC | 124 ms
41,328 KB |
testcase_18 | AC | 108 ms
41,036 KB |
testcase_19 | AC | 122 ms
41,484 KB |
testcase_20 | AC | 110 ms
41,280 KB |
testcase_21 | AC | 116 ms
40,904 KB |
testcase_22 | AC | 123 ms
41,424 KB |
testcase_23 | AC | 127 ms
41,560 KB |
testcase_24 | AC | 129 ms
41,344 KB |
testcase_25 | AC | 106 ms
40,320 KB |
testcase_26 | AC | 133 ms
41,992 KB |
testcase_27 | AC | 120 ms
41,248 KB |
testcase_28 | AC | 233 ms
43,920 KB |
testcase_29 | AC | 264 ms
44,416 KB |
testcase_30 | AC | 254 ms
44,724 KB |
testcase_31 | AC | 244 ms
44,256 KB |
testcase_32 | AC | 239 ms
44,100 KB |
testcase_33 | AC | 230 ms
44,724 KB |
testcase_34 | AC | 241 ms
44,492 KB |
testcase_35 | AC | 232 ms
43,684 KB |
testcase_36 | AC | 236 ms
44,260 KB |
testcase_37 | AC | 248 ms
44,076 KB |
testcase_38 | AC | 912 ms
93,720 KB |
testcase_39 | AC | 1,181 ms
95,168 KB |
testcase_40 | AC | 1,165 ms
95,248 KB |
testcase_41 | AC | 1,082 ms
95,212 KB |
testcase_42 | AC | 1,054 ms
95,008 KB |
testcase_43 | AC | 1,240 ms
95,732 KB |
testcase_44 | AC | 1,144 ms
95,140 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; var sw = new SlidingWindowMaximum(n + 1); 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); sw.clear(); for (int p = 0; p <= max_p; ++p) { sw.push_back(dp[num + p][sum + i * p] - (num + p) * v[i]); if (p > c[i]) sw.pop_front(); dp[num + p][sum + i * p] = sw.query() + (num + p) * v[i]; } } } } for (int i = 1; i <= n; i++) { pw.println(Arrays.stream(dp[i]).max().getAsLong()); } } public static class SlidingWindowMaximum { private int _l, _r; private int _ptr_l, _ptr_r; private long[] _val; private int[] _idx; public SlidingWindowMaximum(int cap) { _val = new long[cap]; _idx = new int[cap]; clear(); } public void push_back(long v) { while (_ptr_l < _ptr_r && _val[_ptr_r - 1] < v) _ptr_r--; _val[_ptr_r] = v; _idx[_ptr_r] = _r; _ptr_r++; _r++; } public void pop_front() { if (_idx[_ptr_l] == _l) _ptr_l++; _l++; } public long query() { return _val[_ptr_l]; } public void clear() { _l = _r = _ptr_l = _ptr_r = 0; } public int size() { return _r - _l; } } }