結果

問題 No.1854 Limited Bubble Sort
ユーザー 👑 colognecologne
提出日時 2022-02-28 14:44:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 1,429 bytes
コンパイル時間 524 ms
コンパイル使用メモリ 10,660 KB
実行使用メモリ 14,344 KB
最終ジャッジ日時 2023-09-20 22:18:04
合計ジャッジ時間 3,121 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,136 KB
testcase_01 AC 21 ms
8,248 KB
testcase_02 AC 21 ms
8,160 KB
testcase_03 AC 20 ms
8,232 KB
testcase_04 AC 19 ms
8,156 KB
testcase_05 AC 20 ms
8,124 KB
testcase_06 AC 17 ms
8,344 KB
testcase_07 AC 19 ms
8,300 KB
testcase_08 AC 18 ms
8,096 KB
testcase_09 AC 17 ms
8,252 KB
testcase_10 AC 18 ms
8,092 KB
testcase_11 AC 50 ms
9,488 KB
testcase_12 AC 42 ms
9,244 KB
testcase_13 AC 73 ms
10,708 KB
testcase_14 AC 45 ms
8,972 KB
testcase_15 AC 36 ms
8,688 KB
testcase_16 AC 78 ms
10,720 KB
testcase_17 AC 77 ms
10,992 KB
testcase_18 AC 81 ms
11,156 KB
testcase_19 AC 78 ms
10,980 KB
testcase_20 AC 82 ms
11,340 KB
testcase_21 AC 62 ms
9,916 KB
testcase_22 AC 107 ms
12,448 KB
testcase_23 AC 68 ms
10,116 KB
testcase_24 AC 136 ms
14,344 KB
testcase_25 AC 19 ms
8,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    T = int(input())
    for i in range(T):
        N = int(input())
        p = [0] + list(map(int, input().split())) + [N+1]
        ans = []
        ok = True

        def swap(i):
            assert i != p[i] and i+1 != p[i+1]
            ans.append(i)
            p[i], p[i+1] = p[i+1], p[i]

        fixed_idx = [i for i in range(N+2) if i == p[i]]
        for i in range(len(fixed_idx)-1):
            f, t = fixed_idx[i]+1, fixed_idx[i+1]-1
            if sorted(p[f:t+1]) != list(range(f, t+1)):
                ok = False
                break

            for i in range(f, t+1):
                for j in range(f, t+1):
                    if p[j] == i:
                        i_idx = j
                        break
                if i_idx == i:
                    continue
                for j in range(i_idx, i, -1):
                    assert p[j] == i
                    k = j-1
                    while p[k] == k+1:
                        k -= 1
                    if p[k] == k:
                        for s in range(j-1, k, -1):
                            swap(s)
                            break
                    else:
                        for s in range(k, j):
                            swap(s)

        if not ok:
            print(-1)
        else:
            assert(sorted(p) == p)
            print(len(ans))
            print(*ans)


if __name__ == '__main__':
    main()
0