-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex01m2.py
53 lines (41 loc) · 1.04 KB
/
ex01m2.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
53
s = 0
def mergeSort(lis,left,right):
if right-left == 1:
return [ lis[left] ]
mid = (left+right)//2
leftLis = mergeSort( lis,left,mid )
rightLis = mergeSort( lis,mid,right )
newLis = []
il = 0
ir = 0
while il != len(leftLis) or ir != len(rightLis):
global s
if il == len(leftLis):
while ir != len(rightLis):
newLis.append( rightLis[ir] )
ir += 1
elif ir == len(rightLis):
while il != len(leftLis):
newLis.append( leftLis[il] )
il += 1
else:
if leftLis[il] <= rightLis[ir]:
newLis.append( leftLis[il] )
il += 1
else:
newLis.append( rightLis[ir] )
s += len(leftLis) - il
ir += 1
return newLis
n = int(input())
lis = [ int(i) for i in input().split() ]
print( mergeSort(lis,0,n) )
print(s)
'''
4
10 30 40 20
6
1 5 4 2 3 -1
7
2 1 3 0 -1 7 6
'''