結果

問題 No.1854 Limited Bubble Sort
ユーザー ああいい
提出日時 2022-02-25 23:46:53
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 921 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 82,732 KB
実行使用メモリ 92,372 KB
最終ジャッジ日時 2024-07-03 18:47:54
合計ジャッジ時間 6,324 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 1 WA * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

T = int(input())


import sys
sys.setrecursionlimit(10 ** 8)

def calc(p,ans):
    N = len(p)
    if N == 1:return True
    while True:
        for i in range(N):
            if p[i] == N:
                index = i
                break
        if index == N-1:
            return calc(p[:-1],ans)
        r = index + 1
        while r < N:
            if p[r] == r + 1:
                return False
            if p[r] == r:
                r += 1
            else:
                break
        if r == N:
            if p[index+1] == index+1:
                r -= 1
            else:
                return False
        for k in range(r-1,index-1,-1):
            ans.append(k+1)
            p[k],p[k+1] = p[k+1],p[k]

    
for _ in range(T):
    N = int(input())
    p = list(map(int,input().split()))
    ans = []
    if calc(p,ans):
        print(len(ans))
        print(*ans)
    else:
        print(-1)
        
0