結果
問題 | No.2026 Yet Another Knapsack Problem |
ユーザー | suisen |
提出日時 | 2022-04-22 14:18:27 |
言語 | Java21 (openjdk 21) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,626 bytes |
コンパイル時間 | 2,376 ms |
コンパイル使用メモリ | 79,416 KB |
実行使用メモリ | 54,364 KB |
最終ジャッジ日時 | 2024-06-27 12:17:18 |
合計ジャッジ時間 | 33,993 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 123 ms
54,364 KB |
testcase_01 | RE | - |
testcase_02 | AC | 125 ms
41,464 KB |
testcase_03 | AC | 189 ms
41,696 KB |
testcase_04 | RE | - |
testcase_05 | AC | 130 ms
41,336 KB |
testcase_06 | AC | 191 ms
41,876 KB |
testcase_07 | AC | 194 ms
41,596 KB |
testcase_08 | AC | 138 ms
41,700 KB |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | AC | 236 ms
41,804 KB |
testcase_12 | AC | 127 ms
41,320 KB |
testcase_13 | RE | - |
testcase_14 | AC | 189 ms
41,832 KB |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | AC | 159 ms
41,504 KB |
testcase_18 | AC | 112 ms
40,360 KB |
testcase_19 | AC | 163 ms
41,832 KB |
testcase_20 | AC | 121 ms
41,464 KB |
testcase_21 | AC | 162 ms
41,156 KB |
testcase_22 | AC | 154 ms
41,364 KB |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | AC | 129 ms
41,192 KB |
testcase_26 | AC | 191 ms
41,656 KB |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | AC | 1,615 ms
44,736 KB |
testcase_30 | AC | 1,614 ms
44,564 KB |
testcase_31 | AC | 1,537 ms
44,716 KB |
testcase_32 | AC | 1,040 ms
44,240 KB |
testcase_33 | RE | - |
testcase_34 | AC | 1,389 ms
44,532 KB |
testcase_35 | AC | 1,048 ms
43,680 KB |
testcase_36 | AC | 1,405 ms
44,272 KB |
testcase_37 | AC | 1,469 ms
44,420 KB |
testcase_38 | TLE | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
ソースコード
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) { 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); for (int i = n; i > 0; i--) { final int max_num = n; 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; } } }