結果

問題 No.1854 Limited Bubble Sort
ユーザー MitarushiMitarushi
提出日時 2021-07-06 20:17:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 832 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 87,048 KB
実行使用メモリ 83,980 KB
最終ジャッジ日時 2023-09-11 20:44:21
合計ジャッジ時間 5,883 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,292 KB
testcase_01 AC 113 ms
77,520 KB
testcase_02 AC 116 ms
77,872 KB
testcase_03 AC 107 ms
77,800 KB
testcase_04 AC 102 ms
77,212 KB
testcase_05 AC 114 ms
77,484 KB
testcase_06 AC 86 ms
76,284 KB
testcase_07 AC 100 ms
76,816 KB
testcase_08 AC 93 ms
76,620 KB
testcase_09 AC 94 ms
76,840 KB
testcase_10 AC 101 ms
76,668 KB
testcase_11 AC 121 ms
80,100 KB
testcase_12 AC 127 ms
79,080 KB
testcase_13 AC 122 ms
81,824 KB
testcase_14 AC 133 ms
78,924 KB
testcase_15 AC 126 ms
77,692 KB
testcase_16 AC 123 ms
82,656 KB
testcase_17 AC 123 ms
82,540 KB
testcase_18 AC 124 ms
82,504 KB
testcase_19 AC 122 ms
82,032 KB
testcase_20 AC 123 ms
82,712 KB
testcase_21 AC 123 ms
79,948 KB
testcase_22 AC 124 ms
82,200 KB
testcase_23 AC 120 ms
81,144 KB
testcase_24 AC 119 ms
83,980 KB
testcase_25 AC 86 ms
75,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def yesno(p):
    for idx, (_, i) in enumerate(p):
        if idx == i and sum(j < i for _, j in p[:idx]) != i:
            return False
    return True


def swap(p, i, ops, comp):
    if p[i][1] == i or p[i+1][1] == i+1:
        return
    if comp(p[i]) > comp(p[i+1]):
        p[i], p[i+1] = p[i+1], p[i]
        ops.append(i+1)


def sort(p, ops, comp):
    for _ in p:
        for i in range(len(p)-1):
            swap(p, i, ops, comp)


def solve():
    n = int(input())
    p = [(i-1 < idx, i-1) for idx, i in enumerate(map(int, input().split()))]

    if not yesno(p):
        print(-1)
        return

    ops = []
    sort(p, ops, lambda x: x)
    sort(p, ops, lambda x: x[1])
    print(len(ops))
    print(" ".join(map(str, ops)))


if __name__ == "__main__":
    t = int(input())
    for _ in range(t):
        solve()
0