-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnt_inv.py
49 lines (41 loc) · 1.05 KB
/
cnt_inv.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
def __merge(arr, l, s, r):
'''
... * * * * * ...
l s r
arr[l, s] sorted
arr[s+1, r] sorted
'''
aux = []
i = l
j = s + 1
cnt_inv = 0
while i <= s and j <= r:
if arr[i] < arr[j]:
aux.append(arr[i])
i += 1
else:
cnt_inv += (s - i + 1) # arr[j, r] >= arr[i]
aux.append(arr[j])
j += 1
while i <= s:
aux.append(arr[i])
i += 1
while j <= r:
aux.append(arr[j])
j += 1
for k in range(l, r + 1):
arr[k] = aux[k - l]
return cnt_inv
def __merge_sort(arr, l, r):
if l >= r:
return 0
m = (l + r) // 2
cnt_left = __merge_sort(arr, l, m)
cnt_right = __merge_sort(arr, m + 1, r)
cnt_split = __merge(arr, l, m, r)
return cnt_left + cnt_right + cnt_split
def merge_sort(arr):
return __merge_sort(arr, 0, len(arr) - 1)
n = int(input())
arr = list(map(int, input().strip().split(' ')))
print(merge_sort(arr))