結果
問題 | No.2026 Yet Another Knapsack Problem |
ユーザー | suisen |
提出日時 | 2022-04-22 02:00:21 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,197 bytes |
コンパイル時間 | 141 ms |
コンパイル使用メモリ | 81,928 KB |
実行使用メモリ | 71,476 KB |
最終ジャッジ日時 | 2024-06-27 12:16:04 |
合計ジャッジ時間 | 4,522 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | RE | - |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | RE | - |
testcase_32 | RE | - |
testcase_33 | RE | - |
testcase_34 | RE | - |
testcase_35 | RE | - |
testcase_36 | RE | - |
testcase_37 | RE | - |
testcase_38 | RE | - |
testcase_39 | RE | - |
testcase_40 | RE | - |
testcase_41 | RE | - |
testcase_42 | RE | - |
testcase_43 | RE | - |
testcase_44 | RE | - |
ソースコード
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, M, W = map(int, input().split()) items = [(int(0), int(0))] for _ in range(N): c, v = map(int, input().split()) items.append((c, v)) INF = 1 << 60 dp = [[-INF] * (W + 1) for i in range(M + 1)] dp[0][0] = 0 sw = SlidingWindowMaximum() for i in reversed(range(1, N + 1)): max_num = min(M, W // i) max_sum = W 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 ans = [0] * (M + 1) for k in range(1, M + 1): ans[k] = max(dp[k]) if ans[k] < -INF // 2: ans[k] = -INF print(ans[k])