結果

問題 No.1854 Limited Bubble Sort
ユーザー chineristACchineristAC
提出日時 2022-02-26 00:17:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 199 ms / 2,000 ms
コード長 1,822 bytes
コンパイル時間 368 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 93,164 KB
最終ジャッジ日時 2023-09-16 20:01:47
合計ジャッジ時間 6,135 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
82,816 KB
testcase_01 AC 154 ms
85,952 KB
testcase_02 AC 142 ms
86,108 KB
testcase_03 AC 145 ms
85,800 KB
testcase_04 AC 146 ms
86,216 KB
testcase_05 AC 141 ms
85,940 KB
testcase_06 AC 123 ms
82,656 KB
testcase_07 AC 135 ms
83,680 KB
testcase_08 AC 132 ms
83,740 KB
testcase_09 AC 135 ms
83,548 KB
testcase_10 AC 136 ms
83,500 KB
testcase_11 AC 153 ms
88,048 KB
testcase_12 AC 163 ms
87,164 KB
testcase_13 AC 157 ms
89,528 KB
testcase_14 AC 164 ms
86,948 KB
testcase_15 AC 162 ms
86,516 KB
testcase_16 AC 160 ms
89,336 KB
testcase_17 AC 160 ms
89,768 KB
testcase_18 AC 162 ms
89,428 KB
testcase_19 AC 159 ms
89,596 KB
testcase_20 AC 160 ms
90,312 KB
testcase_21 AC 163 ms
90,340 KB
testcase_22 AC 171 ms
92,896 KB
testcase_23 AC 162 ms
90,548 KB
testcase_24 AC 199 ms
93,164 KB
testcase_25 AC 141 ms
82,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    return (g1[n] * g2[r] % mod) * g2[n-r] % mod

mod = 998244353
N = 2*10**5
g1 = [1]*(N+1)
g2 = [1]*(N+1)
inverse = [1]*(N+1)

for i in range( 2, N + 1 ):
    g1[i]=( ( g1[i-1] * i ) % mod )
    inverse[i]=( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2[i]=( (g2[i-1] * inverse[i]) % mod )
inverse[0]=0


import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import e, log,gcd

input = lambda :sys.stdin.readline()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

for _ in range(int(input())):
    N = int(input())
    P = li()
    #P = [i+1 for i in range(N)]
    #while any(P[i]==i+1 for i in range(N)):
        #random.shuffle(P)
    res = []
    flag = True
    for i in range(N):
        pos = P.index(i+1)
        for j in range(i,pos):
            if P[j]==j+1:
                flag = False
                break
        if not flag:
            break

        while pos!=i:
            s = -1
            for j in range(i,pos)[::-1]:
                if P[j]!=j+2:
                    s = j
                    break
            if s==-1:
                for j in range(i,pos)[::-1]:
                    res.append(j+1)
                    P[j],P[j+1] = P[j+1],P[j]
                    #print(P)
                pos = i
            else:
                for j in range(s,pos-1):
                    res.append(j+1)
                    P[j],P[j+1] = P[j+1],P[j]
                    #print(P)
                res.append(pos)
                P[pos-1],P[pos] = P[pos],P[pos-1]
                #print(P)
                pos = pos-1
    
    #print(P)
    
    if flag:
        print(len(res))
        print(*res)
    else:
        print(-1)
0