解题思路:归并排序
重点在于想清楚是对哪个数组进行归并排序求逆序对
【Python程序代码】
- from math import *
- n = int(input())
- a = list(map(int,input().split()))
- b = list(map(int,input().split()))
- na,nb = [],[]
- for i in range(n):
- na.append([a[i],i])
- nb.append([b[i],i])
- na.sort()
- nb.sort()
- np = [0]*(n+5)
- for i in range(n):
- np[na[i][1]] = nb[i][1]
- tep = [0]*(100005)
- def merge_sort(q, l, r):
- if l >= r: return 0
- mid = (l + r) >> 1
- res = merge_sort(q, l, mid) + merge_sort(q, mid + 1, r)
- k, i, j = 0, l, mid + 1
- while i <= mid and j <= r:
- if q[i] <= q[j]:
- tep[k] = q[i]
- k, i = k + 1, i + 1
- else:
- res += mid - i + 1
- tep[k] = q[j]
- k, j = k + 1, j + 1
-
- while i <= mid:
- tep[k] = q[i]
- k, i = k + 1, i + 1
- while j <= r:
- tep[k] = q[j]
- k, j = k + 1, j + 1
- j = 0
- for i in range(l, r + 1):
- q[i] = tep[j]
- j += 1
- return res
- print( (merge_sort(np,0,n-1))%99999997)
解题思路:归并排序
归并排序模板题
【Python程序代码】
- n = int(input())
- a = list(map(int,input().split()))
- tep = [0]*(n+5)
- def merge_sort(q,l,r):
- if l>=r:return
- mid = (l+r)>>1
- merge_sort(q,l,mid);merge_sort(q,mid+1,r)
- k,i,j = 0,l,mid+1
- while i<=mid and j<=r:
- if q[i]<=q[j]:
- tep[k] = q[i]
- k,i=k+1,i+1
- else:
- tep[k] = q[j]
- k,j=k+1,j+1
- while i<=mid:
- tep[k]=q[i]
- k,i=k+1,i+1
- while j<=r:
- tep[k]=q[j]
- k,j=k+1,j+1
- j = 0
- for i in range(l,r+1):
- q[i]=tep[j]
- j += 1
-
- merge_sort(a,0,n-1)
- for i in range(n):
- print(a[i],end=" ")
解题思路:归并排序
归并排序求逆序对模板题
【Python程序代码】
- n = int(input())
- a = list(map(int,input().split()))
- tep = [0]*(n+5)
- def merge_sort(q,l,r):
- if l>=r:return 0
- mid = (l+r)>>1
- res = merge_sort(q,l,mid)+merge_sort(q,mid+1,r)
- k,i,j = 0,l,mid+1
- while i<=mid and j<=r:
- if q[i]<=q[j]:
- tep[k] = q[i]
- k,i=k+1,i+1
- else:
- res += mid - i + 1
- tep[k] = q[j]
- k,j=k+1,j+1
-
- while i<=mid:
- tep[k]=q[i]
- k,i=k+1,i+1
- while j<=r:
- tep[k]=q[j]
- k,j=k+1,j+1
- j = 0
- for i in range(l,r+1):
- q[i]=tep[j]
- j += 1
- return res
- print(merge_sort(a,0,n-1))
解题思路:归并排序
归并排序求出每个数与其他数组成的逆序对数,然后求和公式累加
【Python程序代码】
- n = int(input())
- a = list(map(int, input().split()))
- q,tep = [],[[0]*2 for _ in range(n+5) ]
- for i in range(n):q.append( [a[i],i] )
- sum = [0]*(n+5)
- def merge(q,l,r):
- if l>=r:return
- mid = (l+r)>>1
- merge(q,l,mid);merge(q,mid+1,r)
- k,i,j=0,l,mid+1
- while i<=mid and j<=r:
- if q[i][0]<=q[j][0]:
- tep[k]=q[i]
- sum[q[i][1]] += j-mid-1
- k,i = k+1,i+1
- else:
- tep[k]=q[j]
- sum[q[j][1]] += mid-i+1
- k,j = k+1,j+1
- while i<=mid:
- tep[k] = q[i]
- sum[q[i][1]] += j-mid-1
- k,i = k+1,i+1
- while j<=r:
- tep[k] = q[j]
- k,j = k+1,j+1
- j = 0
- for i in range(l,r+1):
- q[i]=tep[j]
- j += 1
- merge(q,0,n-1)
- res = 0
- for i in range(n):
- res += (sum[i])*(sum[i]+1)//2
- print(res)
-
-
解题思路:归并排序
归并排序求逆序对模板题
【Python程序代码】
- import sys
- tep = [0] * (500005)
- def merge_sort(q,l,r):
- if l>=r:return 0
- mid = (l+r)//2
- res = merge_sort(q,l,mid) + merge_sort(q,mid+1,r)
- k,i,j = 0,l,mid+1
- while i<=mid and j<=r:
- if q[i]<=q[j]:
- tep[k]=q[i]
- k,i = k+1,i+1
- else:
- tep[k]=q[j]
- res += mid-i+1
- k,j = k+1,j+1
- while i<=mid:
- tep[k] = q[i]
- k,i = k+1,i+1
- while j<=r:
- tep[k] = q[j]
- k,j = k+1,j+1
- j = 0
- for i in range(l,r+1):
- q[i] = tep[j]
- j += 1
- return res
- n = int(sys.stdin.readline())
- while n!=0:
- a = []
- for i in range(n):
- a.append(int(sys.stdin.readline()))
- print(merge_sort(a,0,n-1))
- n = int(sys.stdin.readline())
-
-