結果
問題 | No.2026 Yet Another Knapsack Problem |
ユーザー | suisen |
提出日時 | 2022-04-22 02:01:50 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 3,945 ms / 10,000 ms |
コード長 | 2,089 bytes |
コンパイル時間 | 413 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 132,208 KB |
最終ジャッジ日時 | 2024-06-27 12:15:58 |
合計ジャッジ時間 | 26,199 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 63 ms
67,328 KB |
testcase_01 | AC | 65 ms
67,584 KB |
testcase_02 | AC | 64 ms
67,612 KB |
testcase_03 | AC | 158 ms
79,320 KB |
testcase_04 | AC | 132 ms
79,744 KB |
testcase_05 | AC | 70 ms
70,912 KB |
testcase_06 | AC | 166 ms
80,092 KB |
testcase_07 | AC | 134 ms
79,352 KB |
testcase_08 | AC | 89 ms
78,080 KB |
testcase_09 | AC | 162 ms
79,408 KB |
testcase_10 | AC | 139 ms
79,228 KB |
testcase_11 | AC | 166 ms
79,340 KB |
testcase_12 | AC | 64 ms
67,584 KB |
testcase_13 | AC | 107 ms
78,440 KB |
testcase_14 | AC | 100 ms
78,408 KB |
testcase_15 | AC | 63 ms
67,328 KB |
testcase_16 | AC | 104 ms
78,412 KB |
testcase_17 | AC | 103 ms
79,104 KB |
testcase_18 | AC | 63 ms
67,712 KB |
testcase_19 | AC | 101 ms
78,384 KB |
testcase_20 | AC | 63 ms
67,072 KB |
testcase_21 | AC | 100 ms
78,460 KB |
testcase_22 | AC | 101 ms
78,720 KB |
testcase_23 | AC | 150 ms
79,028 KB |
testcase_24 | AC | 64 ms
67,584 KB |
testcase_25 | AC | 68 ms
68,480 KB |
testcase_26 | AC | 154 ms
79,492 KB |
testcase_27 | AC | 111 ms
78,516 KB |
testcase_28 | AC | 307 ms
81,604 KB |
testcase_29 | AC | 310 ms
81,684 KB |
testcase_30 | AC | 319 ms
81,312 KB |
testcase_31 | AC | 296 ms
81,348 KB |
testcase_32 | AC | 153 ms
80,000 KB |
testcase_33 | AC | 174 ms
80,856 KB |
testcase_34 | AC | 165 ms
80,660 KB |
testcase_35 | AC | 259 ms
81,068 KB |
testcase_36 | AC | 276 ms
81,668 KB |
testcase_37 | AC | 269 ms
81,096 KB |
testcase_38 | AC | 1,856 ms
129,072 KB |
testcase_39 | AC | 3,924 ms
132,208 KB |
testcase_40 | AC | 3,945 ms
131,208 KB |
testcase_41 | AC | 1,941 ms
128,632 KB |
testcase_42 | AC | 1,995 ms
128,768 KB |
testcase_43 | AC | 2,507 ms
130,452 KB |
testcase_44 | AC | 2,484 ms
129,940 KB |
ソースコード
from collections import deque from typing import Deque, Tuple class SlidingWindowMaximum: l : int r : int dq : Deque[Tuple[int, int]] def __init__(self) -> None: self.clear() def clear(self) -> None: self.l = 0 self.r = 0 self.dq = deque() def push_back(self, v : int) -> None: while self.dq and self.dq[-1][0] < v: self.dq.pop() self.dq.append((v, self.r)) self.r += 1 def pop_front(self) -> None: assert self.size() if self.dq[0][1] == self.l: self.dq.popleft() self.l += 1 def query(self) -> int: return self.dq[0][0] def size(self) -> int: return self.r - self.l N = int(input()) items = [(int(0), int(0))] for _ in range(N): c, v = map(int, input().split()) items.append((c, v)) INF = 1 << 60 dp = [[-INF] * (N + 1) for i in range(N + 1)] dp[0][0] = 0 sw = SlidingWindowMaximum() for i in reversed(range(1, N + 1)): max_num = min(N, N // i) max_sum = N c, v = items[i] # dp[num][sum] = max{ pd[num-p][sum-p*i]-(num-p)*v[i] | 0<=p<=c[i] } ⋃ {-∞} + num*v[i] for num in range(max_num + 1): for sum in range(i if num else max_sum + 1): max_p = min(max_num - num, (max_sum - sum) // i) if max_p <= c: max_val = -INF for p in range(max_p + 1): max_val = max(max_val, dp[num + p][sum + i * p] - (num + p) * v) dp[num + p][sum + i * p] = max_val + (num + p) * v else: sw.clear() for p in range(c + 1): sw.push_back(dp[num + p][sum + i * p] - (num + p) * v) dp[num + p][sum + i * p] = sw.query() + (num + p) * v for p in range(c + 1, max_p + 1): sw.push_back(dp[num + p][sum + i * p] - (num + p) * v) sw.pop_front() dp[num + p][sum + i * p] = sw.query() + (num + p) * v for k in range(1, N + 1): print(max(dp[k]))