結果
問題 | No.1369 交換門松列・竹 |
ユーザー |
![]() |
提出日時 | 2025-03-31 17:32:26 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,304 bytes |
コンパイル時間 | 222 ms |
コンパイル使用メモリ | 82,552 KB |
実行使用メモリ | 90,416 KB |
最終ジャッジ日時 | 2025-03-31 17:33:27 |
合計ジャッジ時間 | 9,452 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 21 WA * 8 TLE * 2 -- * 2 |
ソースコード
def is_kadomatsu(a, b, c):if a == b or b == c or a == c:return Falsereturn (b > a and b > c) or (b < a and b < c)def solve():import sysinput = sys.stdin.read().split()ptr = 0T = int(input[ptr])ptr += 1for _ in range(T):N = int(input[ptr])ptr +=1A = list(map(int, input[ptr:ptr+N]))ptr +=NBAD = []for s in range(N-2):a = A[s]b = A[s+1]c = A[s+2]if not is_kadomatsu(a, b, c):BAD.append(s)# If no BAD, which is impossibleif not BAD:print("No")continue# Collect all positions in BAD tripletsS = set()for s in BAD:S.add(s)S.add(s+1)S.add(s+2)S = list(S)possible = False# Iterate all possible pairs in Sfor i_idx in range(len(S)):i = S[i_idx]for j_idx in range(i_idx+1, len(S)):j = S[j_idx]if A[i] == A[j]:continue# Check if all BAD triplets include i or jall_included = Truefor s in BAD:triplet_includes = Falsefor pos in [s, s+1, s+2]:if pos == i or pos == j:triplet_includes = Truebreakif not triplet_includes:all_included = Falsebreakif not all_included:continue# Perform swap and checkA[i], A[j] = A[j], A[i]# Check BAD triplets are now validvalid_bad = Truefor s in BAD:a_vals = A[s]b_vals = A[s+1]c_vals = A[s+2]if not is_kadomatsu(a_vals, b_vals, c_vals):valid_bad = Falsebreakif not valid_bad:A[i], A[j] = A[j], A[i]continue# Check all triplets involving i or j's positionsvalid = Truechecked = set()for x in [i, j]:for s in [x-2, x-1, x]:if s <0 or s+2 >= N:continueif s not in checked:checked.add(s)a = A[s]b = A[s+1]c = A[s+2]if not is_kadomatsu(a, b, c):valid = Falsebreakif not valid:break# Revert swapA[i], A[j] = A[j], A[i]if valid:possible = Truebreakif possible:breakprint("Yes" if possible else "No")if __name__ == "__main__":solve()