AcWing 104. 货仓选址
原题链接
简单
作者:
将情怀讲泛滥的恶果
,
2021-04-01 09:49:04
,
所有人可见
,
阅读 210
def quick_sort(nums,l,r):
if l>=r:
return
i=l-1
j=r+1
x=nums[(l+r)>>1]
while i<j:
while True:
i+=1
if nums[i]>=x:
break
while True:
j-=1
if nums[j]<=x:
break
if i<j:
temp = nums[i]
nums[i]=nums[j]
nums[j]=temp
quick_sort(nums,l,j)
quick_sort(nums,j+1,r)
if __name__=="__main__":
n=int(input())
nums=[int(x) for x in input().split()]
quick_sort(nums,0,n-1)
target = nums[n//2]
res=0
for i in range(0,n):
res+=abs(target - nums[i])
print(res)