結果

問題 No.1604 Swap Sort:ONE
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-05-10 16:22:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 865 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 78,668 KB
最終ジャッジ日時 2023-09-20 12:31:32
合計ジャッジ時間 3,737 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
72,936 KB
testcase_01 AC 73 ms
72,856 KB
testcase_02 AC 73 ms
72,888 KB
testcase_03 AC 73 ms
72,956 KB
testcase_04 AC 74 ms
72,984 KB
testcase_05 AC 90 ms
78,588 KB
testcase_06 AC 89 ms
78,284 KB
testcase_07 AC 93 ms
78,588 KB
testcase_08 AC 93 ms
78,668 KB
testcase_09 AC 93 ms
78,324 KB
testcase_10 AC 94 ms
78,284 KB
testcase_11 AC 93 ms
78,240 KB
testcase_12 AC 94 ms
78,336 KB
testcase_13 AC 92 ms
78,468 KB
testcase_14 AC 93 ms
78,288 KB
testcase_15 AC 91 ms
78,448 KB
testcase_16 AC 92 ms
78,396 KB
testcase_17 AC 91 ms
78,448 KB
testcase_18 AC 91 ms
78,552 KB
testcase_19 AC 93 ms
78,528 KB
testcase_20 AC 92 ms
78,416 KB
testcase_21 AC 93 ms
78,356 KB
testcase_22 AC 91 ms
78,556 KB
testcase_23 AC 84 ms
77,144 KB
testcase_24 AC 83 ms
77,436 KB
testcase_25 AC 93 ms
78,356 KB
testcase_26 AC 94 ms
78,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

想定解 v2

(数字,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 <= 3000

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