結果

問題 No.2665 Minimize Inversions of Deque
ユーザー titiatitia
提出日時 2024-03-08 21:58:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 229 ms / 2,000 ms
コード長 1,177 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 106,316 KB
最終ジャッジ日時 2024-03-08 21:58:59
合計ジャッジ時間 9,317 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,604 KB
testcase_01 AC 200 ms
80,296 KB
testcase_02 AC 215 ms
81,072 KB
testcase_03 AC 178 ms
80,240 KB
testcase_04 AC 202 ms
80,256 KB
testcase_05 AC 180 ms
80,384 KB
testcase_06 AC 181 ms
80,244 KB
testcase_07 AC 177 ms
80,384 KB
testcase_08 AC 177 ms
80,376 KB
testcase_09 AC 174 ms
80,392 KB
testcase_10 AC 189 ms
80,536 KB
testcase_11 AC 175 ms
80,384 KB
testcase_12 AC 170 ms
80,380 KB
testcase_13 AC 178 ms
80,384 KB
testcase_14 AC 174 ms
80,384 KB
testcase_15 AC 206 ms
80,460 KB
testcase_16 AC 175 ms
80,360 KB
testcase_17 AC 179 ms
80,256 KB
testcase_18 AC 175 ms
80,264 KB
testcase_19 AC 103 ms
77,216 KB
testcase_20 AC 85 ms
77,004 KB
testcase_21 AC 82 ms
76,620 KB
testcase_22 AC 72 ms
76,220 KB
testcase_23 AC 89 ms
76,692 KB
testcase_24 AC 80 ms
76,568 KB
testcase_25 AC 76 ms
76,440 KB
testcase_26 AC 83 ms
76,752 KB
testcase_27 AC 82 ms
76,700 KB
testcase_28 AC 78 ms
76,564 KB
testcase_29 AC 76 ms
76,692 KB
testcase_30 AC 229 ms
103,048 KB
testcase_31 AC 205 ms
90,580 KB
testcase_32 AC 221 ms
98,356 KB
testcase_33 AC 227 ms
104,788 KB
testcase_34 AC 203 ms
90,760 KB
testcase_35 AC 199 ms
106,316 KB
testcase_36 AC 219 ms
106,316 KB
testcase_37 AC 208 ms
89,940 KB
testcase_38 AC 224 ms
96,784 KB
testcase_39 AC 227 ms
106,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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))

0