結果

問題 No.2006 Decrease All to Zero
ユーザー to-omerto-omer
提出日時 2022-07-01 18:28:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,089 ms / 4,000 ms
コード長 2,186 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 81,924 KB
実行使用メモリ 101,516 KB
最終ジャッジ日時 2024-04-26 20:49:16
合計ジャッジ時間 12,724 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,224 KB
testcase_01 AC 43 ms
52,352 KB
testcase_02 AC 150 ms
77,400 KB
testcase_03 AC 40 ms
53,248 KB
testcase_04 AC 40 ms
52,992 KB
testcase_05 AC 38 ms
52,480 KB
testcase_06 AC 88 ms
76,252 KB
testcase_07 AC 99 ms
76,544 KB
testcase_08 AC 46 ms
55,424 KB
testcase_09 AC 45 ms
55,040 KB
testcase_10 AC 1,051 ms
98,636 KB
testcase_11 AC 1,089 ms
88,964 KB
testcase_12 AC 1,033 ms
100,864 KB
testcase_13 AC 1,060 ms
98,688 KB
testcase_14 AC 1,073 ms
89,684 KB
testcase_15 AC 1,025 ms
90,400 KB
testcase_16 AC 541 ms
81,764 KB
testcase_17 AC 832 ms
87,064 KB
testcase_18 AC 38 ms
52,224 KB
testcase_19 AC 39 ms
52,736 KB
testcase_20 AC 40 ms
52,608 KB
testcase_21 AC 41 ms
52,864 KB
testcase_22 AC 40 ms
52,992 KB
testcase_23 AC 39 ms
52,992 KB
testcase_24 AC 39 ms
52,992 KB
testcase_25 AC 39 ms
52,864 KB
testcase_26 AC 1,087 ms
101,516 KB
testcase_27 AC 753 ms
77,520 KB
testcase_28 AC 778 ms
78,336 KB
testcase_29 AC 185 ms
77,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.buffer.readline
INF = 10 ** 18


class QueueAggregation:
    def __init__(self, e, f):
        self.e = e
        self.f = f
        self.front = []
        self.back = []

    def fold_all(self):
        return self.f(
            self.front[-1][0] if self.front else self.e,
            self.back[-1][0] if self.back else self.e,
        )

    def append(self, val):
        x = self.f(
            self.back[-1][0] if self.back else self.e,
            val,
        )
        self.back.append((x, val))

    def appendleft(self, val):
        x = self.f(
            val,
            self.front[-1][0] if self.front else self.e,
        )
        self.front.append((x, val))

    def pop(self):
        if not self.front:
            while self.back:
                _, val = self.back.pop()
                self.appendleft(val)
        return self.front.pop()[1]


def main():
    N = int(input())
    X = list(map(int, input().split()))
    A = list(map(int, input().split()))
    M = max(A) + 1
    DP = [[INF] * M for _ in range(3)]
    EP = [[INF] * M for _ in range(3)]
    for j in range(3):
        DP[j][A[0] * 2 - 2 >> 1] = 0
    for i in range(1, N):
        w = X[i] - X[i - 1]
        for j in range(3):
            for k in range(M):
                DP[j][k] += (k * 2 + 2 - j) * w
                EP[j][k] = INF
        for j in range(3):
            for nj in range(j, 3):
                que = QueueAggregation(INF, min)
                L, R = 0, 0
                for nk in range(M):
                    nL = max(nk - A[i], A[i] - nk - 2 + j, int(j == 2))
                    nR = min(M, nk + A[i] + 1 - nj + j)
                    while L > nL:
                        L -= 1
                        que.appendleft(DP[j][L])
                    while R < nR:
                        que.append(DP[j][R])
                        R += 1
                    while L < nL:
                        que.pop()
                        L += 1
                    EP[nj][nk] = min(EP[nj][nk], que.fold_all())
        DP, EP = EP, DP
    ANS = DP[2][0]
    if ANS >= INF:
        ANS = -1
    print(ANS)


if __name__ == "__main__":
    main()
0