結果

問題 No.1854 Limited Bubble Sort
ユーザー tktk_snsntktk_snsn
提出日時 2022-02-25 21:55:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 992 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 87,180 KB
実行使用メモリ 90,424 KB
最終ジャッジ日時 2023-09-16 16:48:30
合計ジャッジ時間 8,367 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,320 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 79 ms
71,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


def main():
    N = int(input())
    P = list(map(lambda x: int(x) - 1, input().split()))
    pos = [0] * N
    for i, p in enumerate(P):
        pos[p] = i

    ans = []

    def swap(x, y):
        if x > y:
            x, y = y, x
        P[x], P[y] = P[y], P[x]
        pos[P[x]] = x
        pos[P[y]] = y
        ans.append(x + 1)

    def F(L, R):
        if L == R:
            return True
        for x in range(L, R)[::-1]:
            if pos[x] < L or pos[x] >= R:
                return False
            while pos[x] < x:
                i = pos[x]
                swap(i, i+1)
        return True

    L = 0
    for R in range(N):
        if P[R] == R:
            if not F(L, R):
                print(-1)
                return
            L = R + 1
    if not F(L, N):
        print(-1)
        return

    print(len(ans))
    print(*ans)
    return


T = int(input())
for _ in range(T):
    main()
0