-
Notifications
You must be signed in to change notification settings - Fork 0
/
countInversions.py
52 lines (38 loc) · 1.03 KB
/
countInversions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'countInversions' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#
def countInversions(arr):
res = 0
counts = [0]*(len(arr)+1)
rank = { v : i+1 for i, v in enumerate(sorted(arr)) }
for x in reversed(arr):
i = rank[x] - 1
while i:
res += counts[i]
i -= i & -i
i = rank[x]
while i <= len(arr):
counts[i] += 1
i += i & -i
return res
# if __name__ == '__main__':
# fptr = open(os.environ['OUTPUT_PATH'], 'w')
# t = int(input().strip())
# for t_itr in range(t):
# n = int(input().strip())
# arr = list(map(int, input().rstrip().split()))
# result = countInversions(arr)
# fptr.write(str(result) + '\n')
# fptr.close()
arr = [83, 70, 82, 44, 34, 98, 73, 22, 100, 18, 6, 90, 8, 65, 88, 59]
print(arr)
print(countInversions(arr))