結果

問題 No.2665 Minimize Inversions of Deque
ユーザー Yakumo221Yakumo221
提出日時 2024-03-08 22:46:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 316 ms / 2,000 ms
コード長 1,221 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 107,716 KB
最終ジャッジ日時 2024-03-08 22:46:30
合計ジャッジ時間 11,011 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,612 KB
testcase_01 AC 292 ms
77,740 KB
testcase_02 AC 316 ms
77,728 KB
testcase_03 AC 283 ms
77,868 KB
testcase_04 AC 279 ms
77,620 KB
testcase_05 AC 281 ms
77,748 KB
testcase_06 AC 309 ms
77,612 KB
testcase_07 AC 278 ms
77,612 KB
testcase_08 AC 275 ms
77,612 KB
testcase_09 AC 277 ms
77,616 KB
testcase_10 AC 306 ms
77,620 KB
testcase_11 AC 281 ms
77,868 KB
testcase_12 AC 283 ms
77,612 KB
testcase_13 AC 270 ms
77,612 KB
testcase_14 AC 299 ms
77,624 KB
testcase_15 AC 275 ms
77,620 KB
testcase_16 AC 279 ms
77,744 KB
testcase_17 AC 278 ms
77,748 KB
testcase_18 AC 300 ms
77,608 KB
testcase_19 AC 161 ms
77,608 KB
testcase_20 AC 73 ms
76,632 KB
testcase_21 AC 71 ms
76,228 KB
testcase_22 AC 69 ms
76,224 KB
testcase_23 AC 70 ms
76,436 KB
testcase_24 AC 69 ms
76,056 KB
testcase_25 AC 69 ms
76,056 KB
testcase_26 AC 67 ms
76,236 KB
testcase_27 AC 68 ms
76,060 KB
testcase_28 AC 68 ms
76,308 KB
testcase_29 AC 92 ms
76,308 KB
testcase_30 AC 206 ms
103,912 KB
testcase_31 AC 181 ms
90,892 KB
testcase_32 AC 197 ms
96,968 KB
testcase_33 AC 191 ms
97,776 KB
testcase_34 AC 184 ms
89,936 KB
testcase_35 AC 195 ms
107,716 KB
testcase_36 AC 186 ms
107,716 KB
testcase_37 AC 187 ms
89,364 KB
testcase_38 AC 194 ms
97,660 KB
testcase_39 AC 207 ms
107,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        assert i != 0
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


def solve():
    n = int(input())
    plist = list(map(int, input().split()))
    deq = deque()

    bit = Bit(n+10)
    tento = 0
    for i in range(n):
        p = plist[i]
        # print(p)
        if i == 0:
            deq.append(p)
            bit.add(p,1)
            continue
        
        low = bit.sum(p)
        up = i-low

        # print(p, low, up)

        if low > up:
            deq.append(p)
            tento += up
        elif low == up:
            if deq[0] < p:
                deq.append(p)
                tento += up
            else:
                deq.appendleft(p)
                tento += low
        else:
            deq.appendleft(p)
            tento += low
        bit.add(p, 1)

        # print(deq)
    
    print(tento)
    print(*deq)




q = int(input())
while q:
    q -= 1
    solve()
0