結果

問題 No.2665 Minimize Inversions of Deque
ユーザー PNJPNJ
提出日時 2024-03-08 22:17:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 359 ms / 2,000 ms
コード長 1,160 bytes
コンパイル時間 479 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 107,716 KB
最終ジャッジ日時 2024-03-08 22:18:03
合計ジャッジ時間 12,366 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,612 KB
testcase_01 AC 348 ms
78,180 KB
testcase_02 AC 359 ms
77,948 KB
testcase_03 AC 288 ms
77,052 KB
testcase_04 AC 330 ms
77,188 KB
testcase_05 AC 296 ms
78,400 KB
testcase_06 AC 334 ms
77,188 KB
testcase_07 AC 312 ms
77,316 KB
testcase_08 AC 322 ms
77,316 KB
testcase_09 AC 285 ms
77,060 KB
testcase_10 AC 289 ms
77,188 KB
testcase_11 AC 337 ms
77,700 KB
testcase_12 AC 333 ms
77,572 KB
testcase_13 AC 311 ms
77,316 KB
testcase_14 AC 296 ms
77,828 KB
testcase_15 AC 327 ms
77,580 KB
testcase_16 AC 284 ms
77,188 KB
testcase_17 AC 287 ms
77,708 KB
testcase_18 AC 281 ms
77,700 KB
testcase_19 AC 172 ms
77,444 KB
testcase_20 AC 83 ms
76,608 KB
testcase_21 AC 86 ms
76,628 KB
testcase_22 AC 76 ms
76,488 KB
testcase_23 AC 76 ms
76,340 KB
testcase_24 AC 85 ms
76,612 KB
testcase_25 AC 77 ms
76,352 KB
testcase_26 AC 88 ms
76,620 KB
testcase_27 AC 84 ms
76,612 KB
testcase_28 AC 76 ms
76,340 KB
testcase_29 AC 76 ms
76,340 KB
testcase_30 AC 287 ms
103,656 KB
testcase_31 AC 253 ms
90,344 KB
testcase_32 AC 252 ms
93,340 KB
testcase_33 AC 263 ms
101,320 KB
testcase_34 AC 280 ms
89,600 KB
testcase_35 AC 238 ms
107,588 KB
testcase_36 AC 224 ms
107,716 KB
testcase_37 AC 250 ms
92,164 KB
testcase_38 AC 290 ms
97,652 KB
testcase_39 AC 272 ms
107,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
class BIT:
    # 長さN+1の配列を初期化
    def __init__(self, N):
        self.size = N
        self.bit = [0]*(N+1)

    # i番目までの和を求める
    def sum(self, i):
        if i == 0:
            return 0
        res = 0
        while i > 0:
            res += self.bit[i] # フェニック木のi番目の値を加算
            i -= -i & i # 最も右にある1の桁を0にする
        return res

    # i番目の値にxを足して更新する
    def add(self, i, x):
        while i <= self.size:
            self.bit[i] += x # フェニック木のi番目にxを足して更新
            i += -i & i # 最も右にある1の桁に1を足す

T = int(input())
for _ in range(T):
  n = int(input())
  P = list(map(int,input().split()))
  bit = BIT(n)
  x = 0
  A = deque([P[0]])
  bit.add(P[0],1)
  for i in range(1,n):
    p = P[i]
    a =  bit.sum(p)
    b = bit.sum(n) - bit.sum(p)
    if a < b:
      A.appendleft(p)
    elif a > b:
      A.append(p)
    else:
      if A[0] < p:
        A.append(p)
      else:
        A.appendleft(P)
    x += min(a,b)
    bit.add(p,1)
  print(x)
  print(*A)
0