AcWing 107. 超快速排序python3
原题链接
简单
作者:
xanxus1111
,
2020-08-16 15:49:30
,
所有人可见
,
阅读 493
def msort(l,r):
if l >= r: return 0
x = l+r>>1
res = msort(l,x)+msort(x+1,r)
i,j,temp = l,x+1,[]
while i <= x and j <= r:
if q[i] <= q[j]:
temp.append(q[i])
i+=1
else:
temp.append(q[j])
j+=1
res += x - i + 1
while i <= x:
temp.append(q[i])
i+=1
while j <= r:
temp.append(q[j])
j+=1
i, j= l, 0
while i <= r:
q[i] = temp[j]
i+=1
j+=1
return res
while True:
q = []
n = int(input())
if n == 0 : break
for i in range(n):
q.append(int(input()))
print(msort(0,n-1))