結果

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

ソースコード

diff #

import sys
from collections import deque

def main():
    input = sys.stdin.read().split()
    N = int(input[0])
    A = list(map(int, input[1:N+1]))
    
    if N < 3:
        print(0)
        return
    
    q = deque()
    for i in range(N-2):
        j = i + 1
        if A[i] < A[j] and A[j] > A[j+1]:
            q.append(j)
    
    count = 0
    
    while q:
        j = q.popleft()
        if j < 1 or j >= len(A) - 1:
            continue
        if not (A[j-1] < A[j] and A[j] > A[j+1]):
            continue
        
        m = min(A[j-1], A[j+1])
        A[j] = m - 1
        count += 1
        
        i_new = j - 2
        if i_new >= 0:
            if A[i_new] < A[j-1] and A[j-1] > A[j]:
                q.append(j-1)
        
        i_new = j
        if i_new <= len(A) - 3:
            if A[i_new] < A[j+1] and A[j+1] > A[j+2]:
                q.append(j+1)
    
    print(count)

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