結果
問題 | No.1117 数列分割 |
ユーザー | 草苺奶昔 |
提出日時 | 2023-03-10 20:45:15 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,839 ms / 3,000 ms |
コード長 | 2,587 bytes |
コンパイル時間 | 261 ms |
コンパイル使用メモリ | 82,124 KB |
実行使用メモリ | 129,796 KB |
最終ジャッジ日時 | 2024-09-18 03:23:20 |
合計ジャッジ時間 | 21,870 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 54 ms
66,816 KB |
testcase_01 | AC | 61 ms
67,456 KB |
testcase_02 | AC | 61 ms
67,584 KB |
testcase_03 | AC | 596 ms
86,644 KB |
testcase_04 | AC | 230 ms
79,924 KB |
testcase_05 | AC | 60 ms
67,200 KB |
testcase_06 | AC | 133 ms
78,464 KB |
testcase_07 | AC | 129 ms
79,256 KB |
testcase_08 | AC | 356 ms
82,816 KB |
testcase_09 | AC | 320 ms
81,808 KB |
testcase_10 | AC | 365 ms
83,132 KB |
testcase_11 | AC | 625 ms
91,596 KB |
testcase_12 | AC | 909 ms
89,144 KB |
testcase_13 | AC | 728 ms
92,824 KB |
testcase_14 | AC | 822 ms
95,856 KB |
testcase_15 | AC | 1,121 ms
90,524 KB |
testcase_16 | AC | 1,183 ms
96,084 KB |
testcase_17 | AC | 355 ms
84,420 KB |
testcase_18 | AC | 1,644 ms
93,616 KB |
testcase_19 | AC | 1,839 ms
92,772 KB |
testcase_20 | AC | 894 ms
97,208 KB |
testcase_21 | AC | 978 ms
103,640 KB |
testcase_22 | AC | 1,455 ms
129,532 KB |
testcase_23 | AC | 1,537 ms
128,868 KB |
testcase_24 | AC | 1,455 ms
129,796 KB |
testcase_25 | AC | 1,397 ms
128,100 KB |
testcase_26 | AC | 55 ms
67,712 KB |
testcase_27 | AC | 538 ms
85,872 KB |
testcase_28 | AC | 537 ms
85,396 KB |
ソースコード
from itertools import accumulate from typing import Callable, Generic, List, TypeVar E = TypeVar("E") class SlidingWindowAggregation(Generic[E]): """SlidingWindowAggregation Api: 1. append value to tail,O(1). 2. pop value from head,O(1). 3. query aggregated value in window,O(1). """ __slots__ = ["_stack0", "_stack1", "_stack2", "_stack3", "_e0", "_e1", "_size", "_op", "_e"] def __init__(self, e: Callable[[], E], op: Callable[[E, E], E]): """ Args: e: unit element op: merge function """ self._stack0 = [] self._stack1 = [] self._stack2 = [] self._stack3 = [] self._e = e self._e0 = e() self._e1 = e() self._size = 0 self._op = op def append(self, value: E) -> None: if not self._stack0: self._push0(value) self._transfer() else: self._push1(value) self._size += 1 def popleft(self) -> None: if not self._size: return if not self._stack0: self._transfer() self._stack0.pop() self._stack2.pop() self._e0 = self._stack2[-1] if self._stack2 else self._e() self._size -= 1 def query(self) -> E: return self._op(self._e0, self._e1) def _push0(self, value): self._stack0.append(value) self._e0 = self._op(value, self._e0) self._stack2.append(self._e0) def _push1(self, value): self._stack1.append(value) self._e1 = self._op(self._e1, value) self._stack3.append(self._e1) def _transfer(self): while self._stack1: self._push0(self._stack1.pop()) while self._stack3: self._stack3.pop() self._e1 = self._e() def __len__(self): return self._size INF = int(1e18) n, k, m = map(int, input().split()) nums = list(map(int, input().split())) def max(x, y): if x > y: return x return y preSum = [0] + list(accumulate(nums)) dp = [-INF] * (n + 1) dp[0] = 0 for _ in range(k): ndp = [-INF] * (n + 1) s1 = SlidingWindowAggregation(lambda: -INF, max) s2 = SlidingWindowAggregation(lambda: -INF, max) for i in range(n + 1): ndp[i] = max(ndp[i], s1.query() - preSum[i]) ndp[i] = max(ndp[i], s2.query() + preSum[i]) s1.append(dp[i] + preSum[i]) s2.append(dp[i] - preSum[i]) if len(s1) > m: s1.popleft() if len(s2) > m: s2.popleft() dp = ndp print(dp[n])