結果

問題 No.1369 交換門松列・竹
ユーザー gew1fw
提出日時 2025-06-12 18:59:19
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,546 bytes
コンパイル時間 145 ms
コンパイル使用メモリ 82,664 KB
実行使用メモリ 268,280 KB
最終ジャッジ日時 2025-06-12 18:59:26
合計ジャッジ時間 5,659 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 16 WA * 14 TLE * 1 -- * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def is_valid_triplet(a, b, c):
    if a == b or b == c or a == c:
        return False
    return (b > a and b > c) or (b < a and b < c)

def main():
    input = sys.stdin.read().split()
    ptr = 0
    T = int(input[ptr])
    ptr += 1
    for _ in range(T):
        N = int(input[ptr])
        ptr +=1
        A = list(map(int, input[ptr:ptr+N]))
        ptr += N
        
        bad = []
        for i in range(N-2):
            a, b, c = A[i], A[i+1], A[i+2]
            if not is_valid_triplet(a, b, c):
                bad.append(i)
        
        if not bad:
            print("Yes")
            continue
        
        candidates = set()
        for i in bad:
            for x, y in [(i, i+1), (i, i+2), (i+1, i+2)]:
                if x < N and y < N and A[x] != A[y]:
                    if x > y:
                        x, y = y, x
                    candidates.add((x, y))
        
        found = False
        for (x, y) in candidates:
            if x == y:
                continue
            if A[x] == A[y]:
                continue
            
            B = A.copy()
            B[x], B[y] = B[y], B[x]
            
            valid = True
            for i in range(N-2):
                a, b, c = B[i], B[i+1], B[i+2]
                if not is_valid_triplet(a, b, c):
                    valid = False
                    break
            
            if valid:
                found = True
                break
        
        print("Yes" if found else "No")

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