結果

問題 No.1854 Limited Bubble Sort
ユーザー MitarushiMitarushi
提出日時 2022-02-24 23:23:42
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,144 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 82,036 KB
実行使用メモリ 67,584 KB
最終ジャッジ日時 2024-07-02 14:14:57
合計ジャッジ時間 6,291 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 1
other RE * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

from pexpect import EOF


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():
    n = int(input())
    sum_n += 1
    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