-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva00612.py
More file actions
59 lines (50 loc) · 1.08 KB
/
uva00612.py
File metadata and controls
59 lines (50 loc) · 1.08 KB
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
54
55
56
57
58
59
#!/usr/bin/env python3
def sortandcount(s1, s2, b, e):
if e - b < 2:
return 0
ct = 0
m = (b + e)//2
ct += sortandcount(s2, s1, b, m)
ct += sortandcount(s2, s1, m, e)
i = b
j = m
k = b
while i < m and j < e:
if s2[i] <= s2[j]:
s1[k] = s2[i]
i += 1
else:
s1[k] = s2[j]
ct += (m - i)
j += 1
k += 1
while i < m:
s1[k] = s2[i]
i += 1
k += 1
while j < e:
s1[k] = s2[j]
j += 1
k += 1
#print("s1 ",s1)
#print("s2 ",s2)
return ct
def inversion(s):
s1 = list(s)
s2 = list(s)
return sortandcount(s1, s2, 0, len(s))
def main():
M = int(input())
for i in range(M):
input()
_,n = map(int, input().split())
dat = [input() for _ in range(n)]
dat = [(x,inversion(x)) for x in dat]
dat.sort(key=lambda x:x[1])
for x,_ in dat:
print(x)
if i < M-1:
print("")
if __name__ == "__main__":
#inversion('AACATGAAGG')
main()