結果

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

ソースコード

diff #

import sys
from collections import deque

def main():
    n = int(sys.stdin.readline())
    A = list(map(int, sys.stdin.readline().split()))
    if n < 3:
        print(0)
        return
    is_peak = [False] * n
    q = deque()
    
    for i in range(1, n-1):
        if A[i-1] < A[i] and A[i] > A[i+1]:
            is_peak[i] = True
            q.append(i)
    
    count = 0
    
    while q:
        i = q.popleft()
        if not is_peak[i]:
            continue
        # Perform the operation
        min_val = min(A[i-1], A[i+1])
        A[i] = min_val - 1
        count += 1
        is_peak[i] = False  # Current position is no longer a peak
        
        # Check surrounding positions for new peaks
        for j in [i-1, i, i+1]:
            if j < 1 or j >= n-1:
                continue
            # Check if j is a peak now
            if A[j-1] < A[j] and A[j] > A[j+1]:
                if not is_peak[j]:
                    is_peak[j] = True
                    q.append(j)
            else:
                if is_peak[j]:
                    is_peak[j] = False  # In case it was marked as peak before
    
    print(count)

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