本文主要是介绍蓝桥杯刷题--python-19--归并排序,离散化,hash,逆序数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
505. 火柴排队 - AcWing题库
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
mod=99999997
# map
c=[0 for i in range (n+1)]
# 归并排序模板def _MergeSort(arr,l,r,tmp):
if l>=r: return 0
# 分治思想
mid=l+r>>1
# 当前层操作
res=(_MergeSort(arr,l,mid,tmp))+(_MergeSort(arr,mid+1,r,tmp))%mod
# 当前层逻辑
begin1=l
end1=mid
begin2=mid+1
end2=r
index=lwhile(begin1<=end1 and begin2<=end2):
if arr[begin1]<arr[begin2]:
tmp[index]=arr[begin1]
index+=1
begin1+=1
else:
tmp[index]=arr[begin2]
index+=1
begin2+=1
res=(res+end1-begin1+1) % mod# 把多余的添加到数组
# 把多余的添加到数组
while begin1 <= end1:
tmp[index] = arr[begin1]
index += 1
begin1 += 1
while begin2 <= end2:
tmp[index] = arr[begin2]
index += 1
begin2 += 1for i in range (l,r+1):
arr[i]=tmp[i]
return resdef MergeSort(arr):
tmp=[0 for _ in range (len(arr))]
# 调用子函数
res=_MergeSort(arr,0,len(arr)-1,tmp)
return res
# 离散化
def work(a):
n=len(a)
p= [i for i in range (1,n+1)]
p.sort(key=lambda x:a[x-1])
for i in range (1,n+1):
a[p[i-1]-1]=iwork(a)
work(b)
# hash map
for i in range(n):
c[a[i]]=i
for i in range (n):
b[i]=c[b[i]]
print(MergeSort(b))
这篇关于蓝桥杯刷题--python-19--归并排序,离散化,hash,逆序数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!