結果

問題 No.1854 Limited Bubble Sort
ユーザー MitarushiMitarushi
提出日時 2022-02-24 23:25:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,135 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 82,940 KB
最終ジャッジ日時 2023-09-15 10:26:10
合計ジャッジ時間 5,385 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,076 KB
testcase_01 AC 140 ms
77,336 KB
testcase_02 AC 117 ms
77,184 KB
testcase_03 AC 104 ms
77,248 KB
testcase_04 AC 101 ms
76,984 KB
testcase_05 AC 112 ms
77,164 KB
testcase_06 AC 87 ms
75,844 KB
testcase_07 AC 102 ms
76,948 KB
testcase_08 AC 93 ms
76,116 KB
testcase_09 AC 96 ms
76,340 KB
testcase_10 AC 99 ms
76,512 KB
testcase_11 AC 123 ms
79,976 KB
testcase_12 AC 127 ms
78,748 KB
testcase_13 AC 123 ms
81,524 KB
testcase_14 AC 132 ms
78,460 KB
testcase_15 AC 126 ms
77,392 KB
testcase_16 AC 123 ms
82,456 KB
testcase_17 AC 124 ms
82,488 KB
testcase_18 AC 124 ms
82,048 KB
testcase_19 AC 122 ms
81,916 KB
testcase_20 AC 124 ms
82,772 KB
testcase_21 AC 121 ms
79,768 KB
testcase_22 AC 125 ms
81,844 KB
testcase_23 AC 124 ms
80,840 KB
testcase_24 AC 121 ms
82,940 KB
testcase_25 AC 88 ms
74,956 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)


sum_n = 0
def solve():
    global sum_n
    n = int(input())
    sum_n += n
    assert 2 <= n <= 500
    pp = list(map(int, input().split()))
    assert len(pp) == n
    assert list(sorted(pp)) == list(range(1, n + 1))
    p = [(i-1 < idx, i-1) for idx, i in enumerate(pp)]

    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())
    assert 1 <= t <= 250

    for _ in range(t):
        solve()
    
    assert sum_n <= 500

    try:
        x = input()
        assert x == ""
    except EOFError:
        pass
0