結果

問題 No.1369 交換門松列・竹
ユーザー convexineq
提出日時 2021-01-30 09:21:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 431 ms / 2,000 ms
コード長 967 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 82,024 KB
実行使用メモリ 83,072 KB
最終ジャッジ日時 2024-06-27 21:07:45
合計ジャッジ時間 5,611 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

def is_kadomatsu(a,b,c):
    return a!=b and b!=c and c!=a and (b==max(a,b,c) or b==min(a,b,c))

def check(a,res):
    return all(is_kadomatsu(a[i],a[i+1],a[i+2]) for i in res)

def checkv(a,i):
    if i < n-2 and not is_kadomatsu(a[i],a[i+1],a[i+2]): return False
    if 0 <= i-1 < n-2 and not is_kadomatsu(a[i-1],a[i],a[i+1]): return False
    if 0 <= i-2 < n-2 and not is_kadomatsu(a[i-2],a[i-1],a[i]): return False
    return True

def solve(n,a):
    res = [i for i in range(n-2) if not is_kadomatsu(a[i],a[i+1],a[i+2])]
    if len(res) >= 7: return 0
    v = res[0]
    for i in range(v,min(v+3,n)):
        for j in range(n):
            if i==j: continue
            a[i],a[j] = a[j],a[i]
            if check(a,res) and checkv(a,i) and checkv(a,j):
                return 1
            a[i],a[j] = a[j],a[i]
    return 0    

T = int(input())
for _ in range(T):
    n = int(input())
    *a, = map(int,input().split())
    print("Yes" if solve(n,a) else "No")
0