結果

問題 No.1369 交換門松列・竹
ユーザー KudeKude
提出日時 2021-01-29 23:25:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 349 ms / 2,000 ms
コード長 2,649 bytes
コンパイル時間 1,068 ms
コンパイル使用メモリ 86,908 KB
実行使用メモリ 89,776 KB
最終ジャッジ日時 2023-09-09 17:09:45
合計ジャッジ時間 6,594 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,008 KB
testcase_01 AC 72 ms
71,432 KB
testcase_02 AC 72 ms
71,460 KB
testcase_03 AC 72 ms
71,520 KB
testcase_04 AC 72 ms
71,476 KB
testcase_05 AC 72 ms
71,168 KB
testcase_06 AC 71 ms
71,180 KB
testcase_07 AC 72 ms
71,164 KB
testcase_08 AC 71 ms
71,472 KB
testcase_09 AC 70 ms
71,408 KB
testcase_10 AC 71 ms
71,472 KB
testcase_11 AC 72 ms
71,288 KB
testcase_12 AC 72 ms
71,264 KB
testcase_13 AC 349 ms
82,312 KB
testcase_14 AC 306 ms
80,292 KB
testcase_15 AC 129 ms
86,420 KB
testcase_16 AC 142 ms
86,184 KB
testcase_17 AC 140 ms
86,148 KB
testcase_18 AC 141 ms
86,004 KB
testcase_19 AC 132 ms
85,812 KB
testcase_20 AC 94 ms
84,676 KB
testcase_21 AC 129 ms
86,124 KB
testcase_22 AC 139 ms
86,180 KB
testcase_23 AC 138 ms
86,136 KB
testcase_24 AC 93 ms
84,484 KB
testcase_25 AC 187 ms
78,940 KB
testcase_26 AC 181 ms
78,504 KB
testcase_27 AC 186 ms
78,352 KB
testcase_28 AC 91 ms
83,896 KB
testcase_29 AC 90 ms
84,200 KB
testcase_30 AC 96 ms
89,776 KB
testcase_31 AC 97 ms
86,780 KB
testcase_32 AC 173 ms
78,556 KB
testcase_33 AC 139 ms
85,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    for z in range(2):
        n = len(a)
        broken_adj = []
        broken_even = []
        broken_odd = []
        for i in range(n - 1):
            d = a[i+1] - a[i]
            if i & 1 == 0 and d <= 0 or i & 1 == 1 and d >= 0:
                broken_adj.append(i)
        for i in range(n - 2):
            if a[i] == a[i+2]:
                if i & 1 == 0:
                    broken_even.append(i)
                else:
                    broken_odd.append(i)
        if max(map(len, [broken_adj, broken_even, broken_odd])) <= 4:
            if broken_adj:
                s = broken_adj[0]
                t = s + 1
            elif broken_even:
                s = broken_even[0]
                t = s + 2
            else:
                s = broken_odd[0]
                t = s + 2
            for i in (s, t):
                for j in range(z, n):
                    if i == j:
                        continue
                    a[i], a[j] = a[j], a[i]
                    for k in (i, j):
                        if (0 <= k - 2 and a[k-2] == a[k] or
                            k + 2 < n and a[k] == a[k+2] or
                            k & 1 == 0 and (
                                0 <= k - 1 and a[k-1] <= a[k] or
                                k + 1 < n and a[k] >= a[k+1]) or
                            k & 1 == 1 and (
                                0 <= k - 1 and a[k-1] >= a[k] or
                                k + 1 < n and a[k] <= a[k+1]
                            )):
                            break
                    else:
                        ba = set(broken_adj)
                        be = set(broken_even)
                        bo = set(broken_odd)
                        for k in (i, j):
                            if k - 2 in bo:
                                bo.remove(k - 2)
                            if k - 2 in be:
                                be.remove(k - 2)
                            if k - 1 in ba:
                                ba.remove(k - 1)
                            if k in bo:
                                bo.remove(k)
                            if k in be:
                                be.remove(k)
                            if k in ba:
                                ba.remove(k)
                        if max(map(len, [ba, be, bo])) == 0:
                            print('Yes')
                            return
                    a[i], a[j] = a[j], a[i]
        if z == 0:
            a = [0] + a
    print('No')
    return

for _ in range(int(input())):
    solve()
0