結果

問題 No.2665 Minimize Inversions of Deque
ユーザー 👑 rin204rin204
提出日時 2024-03-08 21:07:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 345 ms / 2,000 ms
コード長 1,366 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 107,256 KB
最終ジャッジ日時 2024-03-08 21:07:44
合計ジャッジ時間 11,818 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,460 KB
testcase_01 AC 345 ms
78,260 KB
testcase_02 AC 342 ms
77,720 KB
testcase_03 AC 329 ms
77,740 KB
testcase_04 AC 332 ms
77,492 KB
testcase_05 AC 318 ms
77,364 KB
testcase_06 AC 320 ms
77,376 KB
testcase_07 AC 316 ms
77,364 KB
testcase_08 AC 326 ms
77,360 KB
testcase_09 AC 308 ms
77,600 KB
testcase_10 AC 324 ms
77,376 KB
testcase_11 AC 320 ms
77,356 KB
testcase_12 AC 315 ms
77,604 KB
testcase_13 AC 317 ms
77,368 KB
testcase_14 AC 314 ms
77,424 KB
testcase_15 AC 307 ms
77,488 KB
testcase_16 AC 315 ms
77,372 KB
testcase_17 AC 323 ms
77,376 KB
testcase_18 AC 312 ms
77,368 KB
testcase_19 AC 169 ms
77,368 KB
testcase_20 AC 73 ms
76,220 KB
testcase_21 AC 73 ms
76,268 KB
testcase_22 AC 72 ms
75,968 KB
testcase_23 AC 71 ms
75,832 KB
testcase_24 AC 71 ms
75,852 KB
testcase_25 AC 70 ms
75,832 KB
testcase_26 AC 72 ms
76,108 KB
testcase_27 AC 71 ms
75,848 KB
testcase_28 AC 70 ms
75,836 KB
testcase_29 AC 71 ms
75,840 KB
testcase_30 AC 233 ms
103,532 KB
testcase_31 AC 212 ms
91,272 KB
testcase_32 AC 214 ms
95,940 KB
testcase_33 AC 221 ms
99,920 KB
testcase_34 AC 207 ms
91,612 KB
testcase_35 AC 220 ms
107,256 KB
testcase_36 AC 220 ms
107,256 KB
testcase_37 AC 209 ms
93,244 KB
testcase_38 AC 228 ms
97,140 KB
testcase_39 AC 229 ms
107,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)
        if n == 0:
            self.n0 = 0
        else:
            self.n0 = 1 << (n.bit_length() - 1)

    def sum_(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s

    def sum(self, l, r=-1):
        if r == -1:
            return self.sum_(l)
        else:
            return self.sum_(r) - self.sum_(l)

    def get(self, i):
        return self.sum(i, i + 1)

    def add(self, i, x):
        i += 1
        while i <= self.n:
            self.data[i] += x
            i += i & -i

    def lower_bound(self, x):
        if x <= 0:
            return 0
        i = 0
        k = self.n0
        while k > 0:
            if i + k <= self.n and self.data[i + k] < x:
                x -= self.data[i + k]
                i += k
            k //= 2
        return i + 1


def solve():
    n = int(input())
    P = list(map(int, input().split()))
    ans = 0
    bit = BIT(n + 1)
    L = []
    R = []
    for p in P:
        l = bit.sum(p)
        r = bit.sum(p + 1, n + 1)
        if l < r:
            ans += l
            L.append(p)
        else:
            ans += r
            R.append(p)
        bit.add(p, 1)

    print(ans)
    print(*(L[::-1] + R))


for _ in range(int(input())):
    solve()
0