結果

問題 No.2665 Minimize Inversions of Deque
ユーザー rlangevinrlangevin
提出日時 2024-03-08 21:28:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 1,698 bytes
コンパイル時間 200 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 106,352 KB
最終ジャッジ日時 2024-03-08 21:28:19
合計ジャッジ時間 11,454 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,608 KB
testcase_01 AC 276 ms
84,556 KB
testcase_02 AC 300 ms
84,328 KB
testcase_03 AC 264 ms
84,224 KB
testcase_04 AC 268 ms
83,968 KB
testcase_05 AC 245 ms
83,612 KB
testcase_06 AC 256 ms
83,316 KB
testcase_07 AC 262 ms
83,540 KB
testcase_08 AC 242 ms
83,604 KB
testcase_09 AC 255 ms
83,336 KB
testcase_10 AC 238 ms
83,348 KB
testcase_11 AC 259 ms
83,204 KB
testcase_12 AC 262 ms
83,640 KB
testcase_13 AC 244 ms
83,724 KB
testcase_14 AC 240 ms
83,608 KB
testcase_15 AC 251 ms
83,716 KB
testcase_16 AC 261 ms
83,700 KB
testcase_17 AC 257 ms
83,572 KB
testcase_18 AC 268 ms
83,448 KB
testcase_19 AC 152 ms
77,784 KB
testcase_20 AC 92 ms
76,760 KB
testcase_21 AC 92 ms
76,632 KB
testcase_22 AC 84 ms
76,492 KB
testcase_23 AC 84 ms
76,576 KB
testcase_24 AC 93 ms
76,584 KB
testcase_25 AC 88 ms
76,664 KB
testcase_26 AC 92 ms
76,632 KB
testcase_27 AC 92 ms
76,608 KB
testcase_28 AC 83 ms
76,488 KB
testcase_29 AC 82 ms
76,484 KB
testcase_30 AC 276 ms
102,620 KB
testcase_31 AC 261 ms
91,560 KB
testcase_32 AC 273 ms
97,204 KB
testcase_33 AC 278 ms
99,392 KB
testcase_34 AC 260 ms
90,840 KB
testcase_35 AC 253 ms
106,352 KB
testcase_36 AC 252 ms
106,352 KB
testcase_37 AC 274 ms
89,664 KB
testcase_38 AC 287 ms
96,400 KB
testcase_39 AC 286 ms
106,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class Fenwick_Tree:
    def __init__(self, n):
        self._n = n
        self.data = [0] * n

    def add(self, p, x):
        assert 0 <= p < self._n
        p += 1
        while p <= self._n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, l, r):
        assert 0 <= l <= r <= self._n
        return self._sum(r) - self._sum(l)

    def _sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def get(self, k):
        k += 1
        x, r = 0, 1
        while r < self._n:
            r <<= 1
        len = r
        while len:
            if x + len - 1 < self._n:
                if self.data[x + len - 1] < k:
                    k -= self.data[x + len - 1]
                    x += len
            len >>= 1
        return x

    def __str__(self):
        temp = []
        for i in range(self._n):
            temp.append(str(self.sum(i, i + 1)))
        return ' '.join(temp)


from collections import *
Q = int(input())
for _ in range(Q):
    N = int(input())
    T = Fenwick_Tree(N)
    P = list(map(int, input().split()))
    ans = deque()
    v = 0
    for i in range(N):
        p = P[i] - 1
        L = T.sum(0, p)
        R = T.sum(p+1, N)
        if L < R:
            ans.appendleft(p+1)
            v += L
        elif L > R:
            ans.append(p+1)
            v += R
        elif len(ans) == 0:
            ans.append(p+1)
            v += L
        elif ans[0]-1 < p:
            ans.append(p+1)
            v += L
        else:
            ans.appendleft(p+1)
            v += L
        T.add(p, 1)
    print(v)
    print(*ans)
0