結果
問題 | No.2665 Minimize Inversions of Deque |
ユーザー |
![]() |
提出日時 | 2024-03-08 21:58:50 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 244 ms / 2,000 ms |
コード長 | 1,177 bytes |
コンパイル時間 | 161 ms |
コンパイル使用メモリ | 81,864 KB |
実行使用メモリ | 106,624 KB |
最終ジャッジ日時 | 2024-09-29 19:38:15 |
合計ジャッジ時間 | 9,385 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 40 |
ソースコード
import sys input = sys.stdin.readline from collections import deque def update(v,w): # index vにwを加える while v<=LEN: BIT[v]+=w v+=(v&(-v)) # v&(-v)で、最も下の立っているビット. 自分を含む大きなノードへ. たとえばv=3→v=4 def getvalue(v): # [1,v]の区間の和を求める ANS=0 while v!=0: ANS+=BIT[v] v-=(v&(-v)) # 自分より小さい自分の和を構成するノードへ. たとえばv=14→v=12へ return ANS T=int(input()) for tests in range(T): n=int(input()) P=list(map(int,input().split())) ANS=deque() LEN=n+1 BIT=[0]*(LEN+1) # 1-indexedなtree. 配列BITの長さはLEN+1にしていることに注意。 score=0 for i in range(n): p=P[i] x=getvalue(p) y=i-x if x<y: ANS.appendleft(p) score+=x elif y<x: ANS.append(p) score+=y else: score+=x if len(ANS)==0 or ANS[0]<p: ANS.append(p) else: ANS.appendleft(p) update(p,1) print(score) print(*list(ANS))