結果

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

ソースコード

diff #

import sys

def is_kadomatsu(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 solve():
    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_kadomatsu(a, b, c):
                bad.append(i)
        
        if len(bad) >= 3:
            print("No")
            continue
        
        candidates = set()
        for i in bad:
            for j in [i-1, i, i+1, i+2, i+1+2]:  # i-1, i, i+1, i+2, i+3 (start of next triplet)
                if 0 <= j < N:
                    candidates.add(j)
            for j in [i, i+1, i+2]:
                if 0 <= j < N:
                    candidates.add(j)
            for j in [i-1, i, i+1, i+2, i+3]:
                if 0 <= j < N:
                    candidates.add(j)
        
        candidates = list(candidates)
        found = False
        original = A.copy()
        
        for i in range(len(candidates)):
            for j in range(i + 1, len(candidates)):
                idx1 = candidates[i]
                idx2 = candidates[j]
                if original[idx1] == original[idx2]:
                    continue
                
                A[idx1], A[idx2] = A[idx2], A[idx1]
                
                valid = True
                for k in range(len(A) - 2):
                    a, b, c = A[k], A[k+1], A[k+2]
                    if a == b or b == c or a == c:
                        valid = False
                        break
                    if not ((b > a and b > c) or (b < a and b < c)):
                        valid = False
                        break
                    if not valid:
                        break
                
                if valid:
                    found = True
                    A[idx1], A[idx2] = A[idx2], A[idx1]
                    break
                A[idx1], A[idx2] = A[idx2], A[idx1]
            
            if found:
                break
        
        print("Yes" if found else "No")

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