結果

問題 No.2518 Adjacent Larger
ユーザー titia
提出日時 2023-10-28 01:57:11
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 800 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 14,844 KB
最終ジャッジ日時 2024-09-25 15:57:20
合計ジャッジ時間 3,768 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def calc(A):
    N=len(A)
    if A==[1]*N:
        return "No"

    flag=1

    B=[]
    for a in A:
        if B and B[-1]==1 and a==1:
            continue
        else:
            B.append(a)

    while B and B[0]==1 and B[-1]==1:
        B.pop()

    for i in range(len(B)):
        if B[i]==2 and B[i-1]==2:
            flag=0
        if B[i]==0 and B[i-1]==0:
            flag=0

        if B[i]==1:
            if B[i-1]==B[(i+1)%len(B)]==2:
                flag=0
            if B[i-1]==B[(i+1)%len(B)]==0:
                flag=0

    if flag==0:
        return "No"

    if sum(A)==N:
        return "Yes"
    else:
        return "No"

T=int(input())
for tests in range(T):
    N=int(input())
    A=list(map(int,input().split()))

    print(calc(A))
0