結果

問題 No.1604 Swap Sort:ONE
ユーザー googol_S0googol_S0
提出日時 2021-07-16 21:23:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 489 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 87,284 KB
実行使用メモリ 76,936 KB
最終ジャッジ日時 2023-09-20 12:54:44
合計ジャッジ時間 3,648 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,596 KB
testcase_01 AC 75 ms
71,292 KB
testcase_02 AC 73 ms
71,564 KB
testcase_03 AC 73 ms
71,608 KB
testcase_04 AC 72 ms
71,296 KB
testcase_05 AC 89 ms
76,508 KB
testcase_06 AC 87 ms
76,784 KB
testcase_07 AC 89 ms
76,936 KB
testcase_08 AC 86 ms
76,564 KB
testcase_09 AC 86 ms
76,528 KB
testcase_10 AC 90 ms
76,764 KB
testcase_11 AC 90 ms
76,828 KB
testcase_12 AC 90 ms
76,852 KB
testcase_13 AC 88 ms
76,848 KB
testcase_14 AC 87 ms
76,548 KB
testcase_15 AC 88 ms
76,768 KB
testcase_16 AC 88 ms
76,736 KB
testcase_17 AC 88 ms
76,836 KB
testcase_18 AC 86 ms
76,828 KB
testcase_19 AC 87 ms
76,548 KB
testcase_20 AC 87 ms
76,852 KB
testcase_21 AC 87 ms
76,732 KB
testcase_22 AC 87 ms
76,740 KB
testcase_23 AC 80 ms
75,620 KB
testcase_24 AC 78 ms
75,700 KB
testcase_25 AC 85 ms
76,504 KB
testcase_26 AC 86 ms
76,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
P=list(map(int,input().split()))
Q=[0]*N
for i in range(N):
  Q[P[i]-1]=i
def Binit(B,siz):
  while len(B)<siz+1:
    B.append(0)
  while len(B)>siz+1:
    del B[-1]
  for i in range(siz+1):
    B[i]=0
  B.append(siz)

def Badd(B,a,x):
  z=a
  while z<=B[-1]:
    B[z]+=x
    z+=(z&(-z))

def Bsum(B,a):
  r=0
  z=a
  while z>0:
    r+=B[z]
    z-=(z&(-z))
  return r

BIT=[]
Binit(BIT,N+3)
ANS=0
for i in range(N):
  ANS+=i-Bsum(BIT,Q[i]+1)
  Badd(BIT,Q[i]+1,1)
print(ANS)
0