結果

問題 No.1854 Limited Bubble Sort
ユーザー 👑 tamatotamato
提出日時 2022-02-25 23:03:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 1,503 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 87,472 KB
実行使用メモリ 90,904 KB
最終ジャッジ日時 2023-09-16 18:24:41
合計ジャッジ時間 4,610 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,556 KB
testcase_01 AC 89 ms
77,268 KB
testcase_02 AC 88 ms
77,004 KB
testcase_03 AC 93 ms
77,552 KB
testcase_04 AC 85 ms
77,152 KB
testcase_05 AC 88 ms
77,228 KB
testcase_06 AC 79 ms
76,496 KB
testcase_07 AC 87 ms
77,060 KB
testcase_08 AC 88 ms
77,088 KB
testcase_09 AC 86 ms
76,984 KB
testcase_10 AC 88 ms
77,208 KB
testcase_11 AC 100 ms
80,652 KB
testcase_12 AC 100 ms
79,692 KB
testcase_13 AC 107 ms
83,432 KB
testcase_14 AC 99 ms
78,728 KB
testcase_15 AC 100 ms
78,344 KB
testcase_16 AC 118 ms
83,660 KB
testcase_17 AC 110 ms
83,752 KB
testcase_18 AC 110 ms
84,232 KB
testcase_19 AC 110 ms
83,496 KB
testcase_20 AC 112 ms
84,284 KB
testcase_21 AC 106 ms
82,216 KB
testcase_22 AC 115 ms
87,604 KB
testcase_23 AC 108 ms
82,664 KB
testcase_24 AC 121 ms
90,904 KB
testcase_25 AC 77 ms
71,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    for _ in range(int(input())):
        N = int(input())
        P = [0] + list(map(int, input().split()))

        P_ori = P[:]

        ans = []
        ok = 1
        for p in range(1, N + 1):
            for i in range(1, N+1):
                if P[i] == p:
                    ii = i
                    break
            if ii == p:
                continue

            for i in range(ii - 1, p - 1, -1):
                j = i
                while P[j] == j + 1:
                    j -= 1
                    if j == p - 1:
                        break
                if j != p - 1 and j != i:
                    for jj in range(j, i):
                        ans.append(jj)
                        if P[jj] == jj or P[jj + 1] == jj + 1:
                            ok = 0
                        P[jj], P[jj + 1] = P[jj + 1], P[jj]
                ans.append(i)
                if P[i] == i or P[i + 1] == i + 1:
                    ok = -1
                P[i], P[i + 1] = P[i + 1], P[i]

        if ok == 1:
            print(len(ans))
            print(*ans)
            assert len(ans) <= N ** 2
        else:
            print(-1)

        if ok == 1:
            for i in ans:
                assert P_ori[i] != i and P_ori[i + 1] != i + 1
                P_ori[i], P_ori[i + 1] = P_ori[i + 1], P_ori[i]
            assert P_ori == list(range(N + 1))


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