結果

問題 No.2665 Minimize Inversions of Deque
ユーザー ニックネーム
提出日時 2024-03-08 21:22:05
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 874 ms / 2,000 ms
コード長 577 bytes
コンパイル時間 453 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 31,152 KB
最終ジャッジ日時 2024-09-29 18:56:44
合計ジャッジ時間 21,014 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self,n):
        self.n = n; self.k = [0]*(n+1)
    def a(self,i,x):
        while i<=self.n: self.k[i] += x; i += i&-i
    def s(self,i):
        t = 0
        while i>0: t += self.k[i]; i -= i&-i
        return t
from collections import deque
for _ in range(int(input())):
    n = int(input()); bit = BIT(n); x = 0; dq = deque()
    for i,v in enumerate(map(int,input().split())):
        c = bit.s(v); bit.a(v,1)
        if c<i-c or c==i-c and dq and v<dq[0]: x += c; dq.appendleft(v)
        else: x += i-c; dq.append(v)
    print(x); print(*dq)
0