結果

問題 No.2036 Max Middle
ユーザー gew1fw
提出日時 2025-06-12 14:10:13
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,060 bytes
コンパイル時間 192 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 54,144 KB
最終ジャッジ日時 2025-06-12 14:10:36
合計ジャッジ時間 4,412 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 4 TLE * 1 -- * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    N, *rest = map(int, sys.stdin.read().split())
    A = rest[:N]
    if N < 3:
        print(0)
        return
    q = deque()
    in_queue = set()
    for i in range(N - 2):
        if A[i] < A[i + 1] and A[i + 1] > A[i + 2]:
            q.append(i)
            in_queue.add(i)
    count = 0
    while q:
        i = q.popleft()
        in_queue.discard(i)
        if i < 0 or i >= N - 2:
            continue
        if A[i] < A[i + 1] and A[i + 1] > A[i + 2]:
            new_val = min(A[i], A[i + 2]) - 1
            A[i + 1] = new_val
            count += 1
            if i - 1 >= 0 and (i - 1) not in in_queue:
                if A[i - 1] < A[i] and A[i] > A[i + 1]:
                    q.append(i - 1)
                    in_queue.add(i - 1)
            if i + 1 <= N - 3 and (i + 1) not in in_queue:
                if A[i + 1] < A[i + 2] and A[i + 2] > A[i + 3]:
                    q.append(i + 1)
                    in_queue.add(i + 1)
    print(count)

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