結果
問題 |
No.1117 数列分割
|
ユーザー |
![]() |
提出日時 | 2025-03-20 21:18:58 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,754 bytes |
コンパイル時間 | 160 ms |
コンパイル使用メモリ | 82,716 KB |
実行使用メモリ | 275,904 KB |
最終ジャッジ日時 | 2025-03-20 21:20:01 |
合計ジャッジ時間 | 14,546 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 20 MLE * 6 |
ソースコード
import sys from collections import deque def main(): N, K, M = map(int, sys.stdin.readline().split()) A = list(map(int, sys.stdin.readline().split())) S = [0] * (N + 1) for i in range(1, N+1): S[i] = S[i-1] + A[i-1] INF = -float('inf') dp = [ [INF]*(N+1) for _ in range(K+1) ] dp[0][0] = 0 for j in range(1, K+1): dq1 = deque() # Stores tuples of (i', value) where value is dp[j-1][i'] - S[i'] dq2 = deque() # Stores tuples of (i', value) where value is dp[j-1][i'] + S[i'] for i in range(j, N+1): start_i = max(j-1, i - M) # Check if i-1 is a valid i' for the previous layer if i-1 >= 0 and dp[j-1][i-1] != INF: current_val1 = dp[j-1][i-1] - S[i-1] # Maintain deque1 in descending order while dq1 and dq1[-1][1] <= current_val1: dq1.pop() dq1.append((i-1, current_val1)) current_val2 = dp[j-1][i-1] + S[i-1] while dq2 and dq2[-1][1] <= current_val2: dq2.pop() dq2.append((i-1, current_val2)) # Remove elements from the front that are out of the window while dq1 and dq1[0][0] < start_i: dq1.popleft() while dq2 and dq2[0][0] < start_i: dq2.popleft() max_val1 = dq1[0][1] if dq1 else INF max_val2 = dq2[0][1] if dq2 else INF current_max = max(max_val1 + S[i], max_val2 - S[i]) if current_max != INF: dp[j][i] = current_max else: dp[j][i] = INF print(dp[K][N]) if __name__ == '__main__': main()