結果

問題 No.2665 Minimize Inversions of Deque
ユーザー minimumminimum
提出日時 2024-03-08 21:40:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 372 ms / 2,000 ms
コード長 1,356 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 107,844 KB
最終ジャッジ日時 2024-03-08 21:40:55
合計ジャッジ時間 13,097 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,616 KB
testcase_01 AC 372 ms
78,076 KB
testcase_02 AC 368 ms
77,660 KB
testcase_03 AC 342 ms
77,684 KB
testcase_04 AC 355 ms
77,948 KB
testcase_05 AC 358 ms
77,960 KB
testcase_06 AC 350 ms
77,596 KB
testcase_07 AC 360 ms
77,676 KB
testcase_08 AC 361 ms
78,084 KB
testcase_09 AC 369 ms
77,708 KB
testcase_10 AC 357 ms
78,304 KB
testcase_11 AC 321 ms
77,668 KB
testcase_12 AC 330 ms
77,452 KB
testcase_13 AC 343 ms
77,868 KB
testcase_14 AC 325 ms
78,080 KB
testcase_15 AC 326 ms
77,828 KB
testcase_16 AC 323 ms
77,900 KB
testcase_17 AC 364 ms
77,748 KB
testcase_18 AC 334 ms
77,700 KB
testcase_19 AC 196 ms
77,976 KB
testcase_20 AC 77 ms
76,780 KB
testcase_21 AC 78 ms
76,524 KB
testcase_22 AC 86 ms
76,484 KB
testcase_23 AC 76 ms
76,508 KB
testcase_24 AC 78 ms
76,472 KB
testcase_25 AC 83 ms
76,508 KB
testcase_26 AC 81 ms
76,512 KB
testcase_27 AC 78 ms
76,500 KB
testcase_28 AC 78 ms
76,488 KB
testcase_29 AC 79 ms
76,472 KB
testcase_30 AC 261 ms
103,988 KB
testcase_31 AC 230 ms
90,872 KB
testcase_32 AC 255 ms
97,388 KB
testcase_33 AC 242 ms
101,444 KB
testcase_34 AC 236 ms
90,660 KB
testcase_35 AC 270 ms
107,720 KB
testcase_36 AC 240 ms
107,844 KB
testcase_37 AC 236 ms
89,868 KB
testcase_38 AC 247 ms
97,512 KB
testcase_39 AC 284 ms
107,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


class FenwickTree:

    def __init__(self, N):
        self.N = N
        self.d = [0] * N
    
    def add(self, p, x):
        assert 0 <= p < self.N
        p += 1
        while p <= self.N:
            self.d[p - 1] += x
            p += p & (-p)
    
    def sum0(self, r):
        s = 0
        while r > 0:
            s += self.d[r - 1]
            r -= r & (-r)
        return s
    
    def get_sum(self, l, r):
        assert 0 <= l <= r <= self.N
        return self.sum0(r) - self.sum0(l)


def solve():
    N = int(input())
    P = list(map(lambda x: x - 1, map(int, input().split())))
    fw = FenwickTree(N)
    dq = deque()
    ans = 0

    for i in range(N):
        l = fw.get_sum(0, P[i])
        r = fw.get_sum(P[i], N)
        fw.add(P[i], 1)

        if l < r:
            dq.appendleft(P[i] + 1)
            ans += l
        elif l > r:
            dq.append(P[i] + 1)
            ans += r
        else:
            if i == 0:
                dq.append(P[i] + 1)
                continue
            top = dq.popleft()
            dq.appendleft(top)
            if P[i] < top:
                dq.appendleft(P[i] + 1)
                ans += l
            else:
                dq.append(P[i] + 1)
                ans += r
    print(ans)
    print(*dq)


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