結果

問題 No.1604 Swap Sort:ONE
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-04-26 17:52:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 867 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 86,916 KB
実行使用メモリ 78,404 KB
最終ジャッジ日時 2023-09-20 12:30:03
合計ジャッジ時間 4,608 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
72,892 KB
testcase_01 AC 74 ms
72,884 KB
testcase_02 AC 74 ms
72,800 KB
testcase_03 AC 78 ms
72,804 KB
testcase_04 AC 73 ms
72,796 KB
testcase_05 AC 89 ms
78,152 KB
testcase_06 AC 92 ms
78,372 KB
testcase_07 AC 95 ms
78,404 KB
testcase_08 AC 93 ms
78,048 KB
testcase_09 AC 95 ms
78,248 KB
testcase_10 AC 94 ms
78,132 KB
testcase_11 AC 93 ms
78,136 KB
testcase_12 AC 94 ms
78,320 KB
testcase_13 AC 94 ms
78,212 KB
testcase_14 AC 93 ms
78,244 KB
testcase_15 AC 93 ms
78,316 KB
testcase_16 AC 94 ms
78,240 KB
testcase_17 AC 92 ms
78,156 KB
testcase_18 AC 93 ms
78,348 KB
testcase_19 AC 94 ms
78,400 KB
testcase_20 AC 94 ms
78,344 KB
testcase_21 AC 96 ms
78,096 KB
testcase_22 AC 98 ms
78,096 KB
testcase_23 AC 86 ms
77,344 KB
testcase_24 AC 82 ms
76,996 KB
testcase_25 AC 95 ms
78,136 KB
testcase_26 AC 92 ms
78,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

想定解

(数字,index)
を昇順ソート
転倒数を求める

"""

import sys
from sys import stdin

def bitadd(a,w,bit): #aにwを加える

    a += 1
    x = a
    while x <= (len(bit)-1):
        bit[x] += w
        x += x & (-1 * x)
 
def bitsum(a,bit): #ind 0~aまでの和を求める

    a += 1
    ret = 0
    x = a
    while x > 0:
        ret += bit[x]
        x -= x & (-1 * x)
    return ret

N = int(stdin.readline())

A = list(map(int,stdin.readline().split()))

assert 2 <= N <= 2*(10**5)

B = [A[i] for i in range(N)]
B.sort()
for i in range(N):
    assert B[i] == i+1

num_ind = [] #数字、indexを入れておく
for i in range(N):
    num_ind.append( (A[i] , i) )
    
num_ind.sort()

BIT = [0] * (200100)
ans = 0

for i in range(N):
    ans += i - bitsum( num_ind[i][1] , BIT )
    bitadd( num_ind[i][1]+1 , 1 , BIT)

print (ans)
0