結果

問題 No.2665 Minimize Inversions of Deque
ユーザー ゼットゼット
提出日時 2024-03-08 21:17:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 412 ms / 2,000 ms
コード長 1,082 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 110,280 KB
最終ジャッジ日時 2024-03-08 21:17:47
合計ジャッジ時間 14,003 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,620 KB
testcase_01 AC 364 ms
79,448 KB
testcase_02 AC 403 ms
78,760 KB
testcase_03 AC 317 ms
78,360 KB
testcase_04 AC 313 ms
78,412 KB
testcase_05 AC 341 ms
78,456 KB
testcase_06 AC 320 ms
78,724 KB
testcase_07 AC 311 ms
78,196 KB
testcase_08 AC 321 ms
78,432 KB
testcase_09 AC 327 ms
78,308 KB
testcase_10 AC 310 ms
78,164 KB
testcase_11 AC 314 ms
78,296 KB
testcase_12 AC 353 ms
78,708 KB
testcase_13 AC 314 ms
78,828 KB
testcase_14 AC 333 ms
78,696 KB
testcase_15 AC 340 ms
78,244 KB
testcase_16 AC 315 ms
78,888 KB
testcase_17 AC 317 ms
78,312 KB
testcase_18 AC 315 ms
78,412 KB
testcase_19 AC 185 ms
78,704 KB
testcase_20 AC 113 ms
77,364 KB
testcase_21 AC 107 ms
77,152 KB
testcase_22 AC 102 ms
77,264 KB
testcase_23 AC 96 ms
76,604 KB
testcase_24 AC 104 ms
77,308 KB
testcase_25 AC 99 ms
76,680 KB
testcase_26 AC 105 ms
77,164 KB
testcase_27 AC 108 ms
77,188 KB
testcase_28 AC 97 ms
76,692 KB
testcase_29 AC 94 ms
76,600 KB
testcase_30 AC 412 ms
106,600 KB
testcase_31 AC 374 ms
95,064 KB
testcase_32 AC 381 ms
99,548 KB
testcase_33 AC 411 ms
107,160 KB
testcase_34 AC 366 ms
95,184 KB
testcase_35 AC 320 ms
110,024 KB
testcase_36 AC 307 ms
110,280 KB
testcase_37 AC 386 ms
97,616 KB
testcase_38 AC 406 ms
100,432 KB
testcase_39 AC 394 ms
109,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class segtree:
  def __init__(self,n):
    self.size=1
    while self.size<n:
      self.size*=2
    self.dat=[0]*(self.size*2)
  def update(self,x,a):
    x+=self.size
    self.dat[x]=a
    while x>1:
      x//=2
      self.dat[x]=(self.dat[2*x]+self.dat[2*x+1])
  def querry(self,u,v):
    u+=self.size
    v+=self.size
    score=0
    while u<v:
      if u&1:
        score+=self.dat[u]
        u+=1
      if v&1:
        v-=1
        score+=self.dat[v]
      u//=2
      v//=2
    return score
Q=int(input())
from collections import deque
for _ in range(Q):
  S=deque()
  result=[]
  N=int(input())
  Z=segtree(N+1)
  ans=0
  A=list(map(int,input().split()))
  now=0
  for i in range(N):
    x=A[i]
    p1=Z.querry(0,x)
    p2=Z.querry(x,N+1)
    if p1<p2:
      S.appendleft(x)
      ans+=p1
      now=x
    elif p1>p2:
      S.append(x)
      ans+=p2
    else:
      if x<now:
        now=x
        S.appendleft(x)
        ans+=p1
      else:
        S.append(x)
        ans+=p2
    Z.update(x,1)
  print(ans)
  while S:
    w=S.popleft()
    result.append(w)
  print(*result)
0