結果

問題 No.1854 Limited Bubble Sort
ユーザー MitarushiMitarushi
提出日時 2022-01-04 22:34:13
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 834 bytes
コンパイル時間 357 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 80,632 KB
最終ジャッジ日時 2023-09-11 20:44:34
合計ジャッジ時間 7,061 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,308 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 84 ms
75,276 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