-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathinversions.py
More file actions
37 lines (29 loc) · 754 Bytes
/
inversions.py
File metadata and controls
37 lines (29 loc) · 754 Bytes
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
# Uses python3
import sys
def merge(a, b, count):
res = []
i = j = 0
while i < len(a) and j < len(b):
if a[i] > b[j]:
res.append(b[j])
count[0] += len(a) - i
j += 1
else:
res.append(a[i])
i += 1
res.extend(a[i:])
res.extend(b[j:])
return res
def merge_sort(a, count):
if len(a) < 2:
return a
mid = len(a) // 2
return merge(merge_sort(a[:mid], count), merge_sort(a[mid:], count), count)
def get_number_of_inversions(a):
count = [0]
sorted = merge_sort(a, count)
return count[0]
if __name__ == '__main__':
input = sys.stdin.read()
n, *a = list(map(int, input.split()))
print(get_number_of_inversions(a))