結果

問題 No.1854 Limited Bubble Sort
ユーザー 👑 colognecologne
提出日時 2022-02-26 18:25:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,009 bytes
コンパイル時間 525 ms
コンパイル使用メモリ 10,912 KB
実行使用メモリ 12,212 KB
最終ジャッジ日時 2023-09-17 20:42:07
合計ジャッジ時間 16,575 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,124 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 22 ms
8,236 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 534 ms
9,276 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 337 ms
8,716 KB
testcase_15 WA -
testcase_16 AC 1,330 ms
10,508 KB
testcase_17 AC 1,379 ms
10,564 KB
testcase_18 AC 1,443 ms
10,764 KB
testcase_19 AC 1,311 ms
10,496 KB
testcase_20 WA -
testcase_21 AC 351 ms
9,588 KB
testcase_22 AC 883 ms
11,096 KB
testcase_23 AC 418 ms
9,716 KB
testcase_24 AC 1,315 ms
12,212 KB
testcase_25 AC 19 ms
7,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

        m = [0 for i in range(N+2)]
        for i in range(1, N+2):
            m[i] = max(m[i-1], p[i])

        ans = []

        def swap(i):
            if i == p[i] or i+1 == p[i+1]:
                return False
            if i+1 == p[i] and max(m[i-1], p[i+1]) != i:
                return False
            if i == p[i+1] and m[i-1] != i-1:
                return False

            ans.append(i)
            p[i], p[i+1] = p[i+1], p[i]
            m[i] = max(m[i-1], p[i])
            m[i+1] = max(m[i], p[i+1])
            return True

        while True:
            for i in range(1, N):
                if p[i] > p[i+1] and swap(i):
                    break
            else:
                break

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


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