結果
問題 | No.2026 Yet Another Knapsack Problem |
ユーザー | suisen |
提出日時 | 2022-04-22 14:20:32 |
言語 | Java21 (openjdk 21) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,630 bytes |
コンパイル時間 | 2,242 ms |
コンパイル使用メモリ | 79,684 KB |
実行使用メモリ | 115,280 KB |
最終ジャッジ日時 | 2024-06-27 12:17:52 |
合計ジャッジ時間 | 33,075 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 123 ms
46,944 KB |
testcase_01 | AC | 127 ms
41,580 KB |
testcase_02 | AC | 126 ms
41,300 KB |
testcase_03 | AC | 188 ms
41,800 KB |
testcase_04 | AC | 178 ms
41,568 KB |
testcase_05 | AC | 125 ms
40,544 KB |
testcase_06 | AC | 195 ms
41,552 KB |
testcase_07 | AC | 184 ms
41,620 KB |
testcase_08 | AC | 125 ms
40,268 KB |
testcase_09 | AC | 213 ms
41,892 KB |
testcase_10 | AC | 164 ms
41,492 KB |
testcase_11 | AC | 211 ms
42,036 KB |
testcase_12 | AC | 126 ms
41,240 KB |
testcase_13 | AC | 189 ms
41,712 KB |
testcase_14 | AC | 183 ms
41,776 KB |
testcase_15 | AC | 121 ms
40,728 KB |
testcase_16 | AC | 178 ms
41,728 KB |
testcase_17 | AC | 161 ms
41,868 KB |
testcase_18 | AC | 131 ms
41,352 KB |
testcase_19 | AC | 164 ms
42,072 KB |
testcase_20 | AC | 122 ms
41,516 KB |
testcase_21 | AC | 159 ms
41,504 KB |
testcase_22 | AC | 163 ms
41,736 KB |
testcase_23 | AC | 180 ms
41,808 KB |
testcase_24 | AC | 124 ms
41,100 KB |
testcase_25 | AC | 132 ms
41,348 KB |
testcase_26 | AC | 190 ms
41,772 KB |
testcase_27 | AC | 170 ms
41,768 KB |
testcase_28 | AC | 1,458 ms
44,484 KB |
testcase_29 | AC | 1,589 ms
44,740 KB |
testcase_30 | AC | 1,650 ms
44,504 KB |
testcase_31 | AC | 1,547 ms
44,492 KB |
testcase_32 | AC | 1,008 ms
44,304 KB |
testcase_33 | AC | 1,516 ms
44,956 KB |
testcase_34 | AC | 1,370 ms
44,564 KB |
testcase_35 | AC | 1,069 ms
44,076 KB |
testcase_36 | AC | 1,385 ms
44,600 KB |
testcase_37 | AC | 1,305 ms
44,472 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 + 1); 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; } } }