結果

問題 No.1854 Limited Bubble Sort
ユーザー chineristACchineristAC
提出日時 2022-02-26 00:15:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,715 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 87,048 KB
実行使用メモリ 95,216 KB
最終ジャッジ日時 2023-09-16 19:58:22
合計ジャッジ時間 8,489 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
82,800 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 126 ms
82,676 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()
    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):
                    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