結果

問題 No.2665 Minimize Inversions of Deque
ユーザー miya145592miya145592
提出日時 2024-03-08 22:57:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 385 ms / 2,000 ms
コード長 1,988 bytes
コンパイル時間 423 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 108,984 KB
最終ジャッジ日時 2024-03-08 22:58:05
合計ジャッジ時間 12,597 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,604 KB
testcase_01 AC 256 ms
81,432 KB
testcase_02 AC 300 ms
83,484 KB
testcase_03 AC 241 ms
81,912 KB
testcase_04 AC 258 ms
82,040 KB
testcase_05 AC 243 ms
81,720 KB
testcase_06 AC 242 ms
82,420 KB
testcase_07 AC 239 ms
81,908 KB
testcase_08 AC 246 ms
82,420 KB
testcase_09 AC 244 ms
82,488 KB
testcase_10 AC 254 ms
82,400 KB
testcase_11 AC 244 ms
82,132 KB
testcase_12 AC 244 ms
81,960 KB
testcase_13 AC 251 ms
81,596 KB
testcase_14 AC 248 ms
82,000 KB
testcase_15 AC 249 ms
82,432 KB
testcase_16 AC 241 ms
81,992 KB
testcase_17 AC 262 ms
82,508 KB
testcase_18 AC 253 ms
81,684 KB
testcase_19 AC 154 ms
78,148 KB
testcase_20 AC 104 ms
77,240 KB
testcase_21 AC 106 ms
77,192 KB
testcase_22 AC 99 ms
76,852 KB
testcase_23 AC 99 ms
76,820 KB
testcase_24 AC 108 ms
77,480 KB
testcase_25 AC 99 ms
76,708 KB
testcase_26 AC 109 ms
77,252 KB
testcase_27 AC 107 ms
77,224 KB
testcase_28 AC 97 ms
76,712 KB
testcase_29 AC 96 ms
76,708 KB
testcase_30 AC 370 ms
105,632 KB
testcase_31 AC 342 ms
93,088 KB
testcase_32 AC 360 ms
97,444 KB
testcase_33 AC 365 ms
104,468 KB
testcase_34 AC 381 ms
94,552 KB
testcase_35 AC 298 ms
108,984 KB
testcase_36 AC 284 ms
108,980 KB
testcase_37 AC 385 ms
94,828 KB
testcase_38 AC 372 ms
99,648 KB
testcase_39 AC 374 ms
108,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegTree:
    def __init__(self, op, e, n, v=None):
        self._n = n
        self._op = op
        self._e = e
        self._log = (n - 1).bit_length()
        self._size = 1 << self._log
        self._d = [self._e()] * (self._size << 1)
        if v is not None:
            for i in range(self._n):
                self._d[self._size + i] = v[i]
            for i in range(self._size - 1, 0, -1):
                self._d[i] = self._op(self._d[i << 1], self._d[i << 1 | 1])

    def set(self, p, x):
        p += self._size
        self._d[p] = x
        while p:
            l, r = p, p^1
            if l > r: l, r = r, l
            self._d[p >> 1] = self._op(self._d[l], self._d[r])
            p >>= 1

    def get(self, p):
        return self._d[p + self._size]

    #[l, r)の区間で求める
    def prod(self, l, r):
        sml, smr = self._e(), self._e()
        l += self._size
        r += self._size
        while l < r:
            if l & 1:
                sml = self._op(sml, self._d[l])
                l += 1
            if r & 1:
                r -= 1
                smr = self._op(self._d[r], smr)
            l >>= 1
            r >>= 1
        return self._op(sml, smr)

    def all_prod(self):
        return self._d[1]

def op(x, y):
    return x+y

def e():
    return 0

from collections import deque
import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
    N = int(input())
    P = list(map(int, input().split()))
    deq = deque()
    ST = SegTree(op, e, N)
    x = 0
    for i, p in enumerate(P):
        cnt = ST.prod(0, p)
        small = cnt
        large = i-cnt
        if small<large:
            deq.appendleft(p)
            x+=small
        elif small>large:
            deq.append(p)
            x+=large
        else:
            if len(deq)>0 and deq[0]>p:
                deq.appendleft(p)
            else:
                deq.append(p)
            x+=small
        ST.set(p-1, 1)
    print(x)
    print(*deq)
0