結果

問題 No.2665 Minimize Inversions of Deque
ユーザー 寝癖寝癖
提出日時 2024-03-08 21:29:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 422 ms / 2,000 ms
コード長 1,896 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 109,960 KB
最終ジャッジ日時 2024-03-08 21:29:30
合計ジャッジ時間 13,598 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
65,608 KB
testcase_01 AC 364 ms
78,956 KB
testcase_02 AC 422 ms
80,748 KB
testcase_03 AC 370 ms
79,424 KB
testcase_04 AC 351 ms
79,336 KB
testcase_05 AC 358 ms
79,096 KB
testcase_06 AC 371 ms
79,588 KB
testcase_07 AC 384 ms
79,064 KB
testcase_08 AC 365 ms
79,328 KB
testcase_09 AC 383 ms
79,956 KB
testcase_10 AC 362 ms
79,204 KB
testcase_11 AC 386 ms
79,216 KB
testcase_12 AC 394 ms
79,232 KB
testcase_13 AC 376 ms
79,220 KB
testcase_14 AC 358 ms
79,104 KB
testcase_15 AC 388 ms
79,328 KB
testcase_16 AC 359 ms
79,104 KB
testcase_17 AC 375 ms
79,104 KB
testcase_18 AC 384 ms
79,108 KB
testcase_19 AC 212 ms
79,412 KB
testcase_20 AC 99 ms
78,028 KB
testcase_21 AC 101 ms
78,028 KB
testcase_22 AC 102 ms
78,036 KB
testcase_23 AC 99 ms
77,896 KB
testcase_24 AC 99 ms
78,028 KB
testcase_25 AC 126 ms
77,904 KB
testcase_26 AC 101 ms
78,028 KB
testcase_27 AC 100 ms
78,028 KB
testcase_28 AC 101 ms
78,028 KB
testcase_29 AC 107 ms
78,668 KB
testcase_30 AC 295 ms
104,324 KB
testcase_31 AC 261 ms
92,564 KB
testcase_32 AC 277 ms
95,056 KB
testcase_33 AC 275 ms
98,900 KB
testcase_34 AC 260 ms
94,492 KB
testcase_35 AC 296 ms
109,960 KB
testcase_36 AC 261 ms
109,944 KB
testcase_37 AC 270 ms
90,904 KB
testcase_38 AC 294 ms
98,288 KB
testcase_39 AC 317 ms
108,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import types

_atcoder_code = """
# Python port of AtCoder Library.

__version__ = '0.0.1'
"""

atcoder = types.ModuleType('atcoder')
exec(_atcoder_code, atcoder.__dict__)

_atcoder_fenwicktree_code = """
import typing


class FenwickTree:
    '''Reference: https://en.wikipedia.org/wiki/Fenwick_tree'''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.data = [0] * n

    def add(self, p: int, x: typing.Any) -> None:
        assert 0 <= p < self._n

        p += 1
        while p <= self._n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, left: int, right: int) -> typing.Any:
        assert 0 <= left <= right <= self._n

        return self._sum(right) - self._sum(left)

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

        return s
"""

atcoder.fenwicktree = types.ModuleType('atcoder.fenwicktree')
exec(_atcoder_fenwicktree_code, atcoder.fenwicktree.__dict__)
FenwickTree = atcoder.fenwicktree.FenwickTree

# from atcoder.fenwicktree import FenwickTree
from collections import deque

T = int(input())

def solve(n, P):
    ft = FenwickTree(n+1)
    ans = deque()
    cnt = 0
    for p in P:
        s = ft.sum(0, p)
        g = ft.sum(p+1, n+1)
        if s < g:
            ans.appendleft(p)
            cnt += s
        elif s > g:
            ans.append(p)
            cnt += g
        else:
            if len(ans) == 0:
                ans.appendleft(p)
                cnt += s
            else:
                if p < ans[0]:
                    ans.appendleft(p)
                    cnt += s
                else:
                    ans.append(p)
                    cnt += g
        ft.add(p, 1)
    
    print(cnt)
    print(*ans)

for _ in range(T):
    n = int(input())
    P = list(map(int, input().split()))
    solve(n, P)
0