結果

問題 No.2036 Max Middle
ユーザー gew1fw
提出日時 2025-06-12 16:19:50
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 975 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 82,672 KB
実行使用メモリ 278,868 KB
最終ジャッジ日時 2025-06-12 16:20:27
合計ジャッジ時間 4,259 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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
    
    dq = deque()
    
    # Identify initial peaks
    for j in range(1, N-1):
        if A[j-1] < A[j] and A[j] > A[j+1]:
            dq.append(j)
    
    operations = 0
    
    while dq:
        j = dq.popleft()
        
        # Check if j is still a peak
        if j <= 0 or j >= N-1:
            continue
        if not (A[j-1] < A[j] and A[j] > A[j+1]):
            continue
        
        # Process the peak
        new_val = min(A[j-1], A[j+1]) - 1
        A[j] = new_val
        operations += 1
        
        # Check left and right neighbors
        for k in [j-1, j+1]:
            if 1 <= k <= N-2:
                if A[k-1] < A[k] and A[k] > A[k+1]:
                    dq.append(k)
    
    print(operations)

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