結果

問題 No.1117 数列分割
ユーザー first_vil
提出日時 2020-07-04 23:21:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,031 ms / 3,000 ms
コード長 1,143 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 162,460 KB
最終ジャッジ日時 2024-11-30 03:19:48
合計ジャッジ時間 17,850 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
def main():
    n,k,m=map(int,input().split())
    a=list(map(int,input().split()))+[0]
    for i in range(n-1,-1,-1):
        a[i]+=a[i+1]
    plus=[deque([]) for j in range(k)]
    plus[0].append((-1,a[0]))
    minus=[deque([]) for j in range(k)]
    minus[0].append((-1,-a[0]))
    for i in range(n):
       for j in range(k-1,-1,-1):
            res=0
            while plus[j] and plus[j][0][0]<i-m:
                plus[j].popleft()
            while minus[j] and minus[j][0][0]<i-m:
                minus[j].popleft()
            if plus[j]:
                res=max(res,plus[j][0][1]-a[i+1])
            if minus[j]:
                res=max(res,minus[j][0][1]+a[i+1])
            if j+1<k:
                while plus[j+1] and plus[j+1][-1][1]<=res+a[i+1]:
                    plus[j+1].pop()
                while minus[j+1] and minus[j+1][-1][1]<=res-a[i+1]:
                    minus[j+1].pop()
                plus[j+1].append((i,res+a[i+1]))
                minus[j+1].append((i,res-a[i+1]))
            elif i==n-1:
                print(res)
                exit()
if __name__=='__main__':
    main()
0