結果
| 問題 | 
                            No.2518 Adjacent Larger
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2023-10-27 22:21:37 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,576 ms / 2,000 ms | 
| コード長 | 760 bytes | 
| コンパイル時間 | 356 ms | 
| コンパイル使用メモリ | 82,376 KB | 
| 実行使用メモリ | 110,016 KB | 
| 最終ジャッジ日時 | 2024-09-25 14:25:00 | 
| 合計ジャッジ時間 | 18,623 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 28 | 
ソースコード
from heapq import heappop,heappush
def solve():
    n = int(input())
    A = list(map(int,input().split()))
    use = [0]*n
    h = []
    for i,a in enumerate(A):
        heappush(h,[a,i])
    while h:
        c,ind = heappop(h)
        if A[ind]!= c:
            continue
        if A[ind]:
            return "No"
        use[ind] = 1
        ni = (ind+1)%n
        if use[ni] == 0:
            A[ni] -= 1
            if A[ni] < 0:
                return "No"
            heappush(h,[A[ni],ni])
        ni = (ind-1)%n
        if use[ni] == 0:
            A[ni] -= 1
            if A[ni] < 0:
                return "No"
            heappush(h,[A[ni],ni])
    return "Yes" if sum(A) == 0 else "No"
t = int(input())
for _ in range(t):
    print(solve())