結果

問題 No.2665 Minimize Inversions of Deque
ユーザー suisensuisen
提出日時 2023-12-28 22:02:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 355 ms / 2,000 ms
コード長 987 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 107,712 KB
最終ジャッジ日時 2024-01-05 15:03:01
合計ジャッジ時間 10,842 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,608 KB
testcase_01 AC 294 ms
77,292 KB
testcase_02 AC 355 ms
79,364 KB
testcase_03 AC 250 ms
77,188 KB
testcase_04 AC 259 ms
77,204 KB
testcase_05 AC 255 ms
77,320 KB
testcase_06 AC 252 ms
77,192 KB
testcase_07 AC 253 ms
77,320 KB
testcase_08 AC 259 ms
77,196 KB
testcase_09 AC 255 ms
77,320 KB
testcase_10 AC 257 ms
77,184 KB
testcase_11 AC 254 ms
77,320 KB
testcase_12 AC 265 ms
77,192 KB
testcase_13 AC 263 ms
77,192 KB
testcase_14 AC 256 ms
77,188 KB
testcase_15 AC 260 ms
77,184 KB
testcase_16 AC 265 ms
77,192 KB
testcase_17 AC 270 ms
77,312 KB
testcase_18 AC 257 ms
77,184 KB
testcase_19 AC 139 ms
77,320 KB
testcase_20 AC 95 ms
76,632 KB
testcase_21 AC 100 ms
76,764 KB
testcase_22 AC 88 ms
76,472 KB
testcase_23 AC 72 ms
76,584 KB
testcase_24 AC 92 ms
76,712 KB
testcase_25 AC 79 ms
76,840 KB
testcase_26 AC 98 ms
76,744 KB
testcase_27 AC 88 ms
76,732 KB
testcase_28 AC 81 ms
76,712 KB
testcase_29 AC 76 ms
76,840 KB
testcase_30 AC 254 ms
103,868 KB
testcase_31 AC 230 ms
92,036 KB
testcase_32 AC 238 ms
97,676 KB
testcase_33 AC 245 ms
101,980 KB
testcase_34 AC 224 ms
91,596 KB
testcase_35 AC 195 ms
107,712 KB
testcase_36 AC 192 ms
107,712 KB
testcase_37 AC 229 ms
89,544 KB
testcase_38 AC 235 ms
97,888 KB
testcase_39 AC 247 ms
107,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


t = int(input())
for _ in range(t):
    n = int(input())
    p = list(map(lambda x: int(x) - 1, input().split()))

    ft = [0] * n
    def ft_add(p: int, v: int):
        p += 1
        while p <= n:
            ft[p - 1] += v
            p += -p & p
    def ft_sum(r: int):
        s = 0
        while r:
            s += ft[r - 1]
            r -= -r & r
        return s

    inv = 0
    a = deque()
    for i, v in enumerate(p):
        l = ft_sum(p[i])
        r = i - l
        ft_add(p[i], 1)
        ok_l = l <= r
        ok_r = l >= r
        if ok_l and ok_r:
            if a and a[0] < p[i]:
                a.append(p[i])
            else:
                a.appendleft(p[i])
        elif ok_l:
            a.appendleft(p[i])
        elif ok_r:
            a.append(p[i])
        else:
            assert False
        inv += min(l, r)
    
    ans = list(a)
    
    for i in range(n):
        ans[i] += 1
    
    print(inv)
    print(*ans)
0