• 2024蓝桥杯每日一题(归并排序)


    一、第一题:火柴排队

    解题思路:归并排序
            重点在于想清楚是对哪个数组进行归并排序求逆序对

    【Python程序代码】

    1. from math import *
    2. n = int(input())
    3. a = list(map(int,input().split()))
    4. b = list(map(int,input().split()))
    5. na,nb = [],[]
    6. for i in range(n):
    7. na.append([a[i],i])
    8. nb.append([b[i],i])
    9. na.sort()
    10. nb.sort()
    11. np = [0]*(n+5)
    12. for i in range(n):
    13. np[na[i][1]] = nb[i][1]
    14. tep = [0]*(100005)
    15. def merge_sort(q, l, r):
    16. if l >= r: return 0
    17. mid = (l + r) >> 1
    18. res = merge_sort(q, l, mid) + merge_sort(q, mid + 1, r)
    19. k, i, j = 0, l, mid + 1
    20. while i <= mid and j <= r:
    21. if q[i] <= q[j]:
    22. tep[k] = q[i]
    23. k, i = k + 1, i + 1
    24. else:
    25. res += mid - i + 1
    26. tep[k] = q[j]
    27. k, j = k + 1, j + 1
    28. while i <= mid:
    29. tep[k] = q[i]
    30. k, i = k + 1, i + 1
    31. while j <= r:
    32. tep[k] = q[j]
    33. k, j = k + 1, j + 1
    34. j = 0
    35. for i in range(l, r + 1):
    36. q[i] = tep[j]
    37. j += 1
    38. return res
    39. print( (merge_sort(np,0,n-1))%99999997)

    二、第二题: 归并排序

    解题思路:归并排序
            归并排序模板题

    【Python程序代码】

    1. n = int(input())
    2. a = list(map(int,input().split()))
    3. tep = [0]*(n+5)
    4. def merge_sort(q,l,r):
    5. if l>=r:return
    6. mid = (l+r)>>1
    7. merge_sort(q,l,mid);merge_sort(q,mid+1,r)
    8. k,i,j = 0,l,mid+1
    9. while i<=mid and j<=r:
    10. if q[i]<=q[j]:
    11. tep[k] = q[i]
    12. k,i=k+1,i+1
    13. else:
    14. tep[k] = q[j]
    15. k,j=k+1,j+1
    16. while i<=mid:
    17. tep[k]=q[i]
    18. k,i=k+1,i+1
    19. while j<=r:
    20. tep[k]=q[j]
    21. k,j=k+1,j+1
    22. j = 0
    23. for i in range(l,r+1):
    24. q[i]=tep[j]
    25. j += 1
    26. merge_sort(a,0,n-1)
    27. for i in range(n):
    28. print(a[i],end=" ")

    三、第三题:逆序对的数量

    解题思路:归并排序
            归并排序求逆序对模板题

    【Python程序代码】

    1. n = int(input())
    2. a = list(map(int,input().split()))
    3. tep = [0]*(n+5)
    4. def merge_sort(q,l,r):
    5. if l>=r:return 0
    6. mid = (l+r)>>1
    7. res = merge_sort(q,l,mid)+merge_sort(q,mid+1,r)
    8. k,i,j = 0,l,mid+1
    9. while i<=mid and j<=r:
    10. if q[i]<=q[j]:
    11. tep[k] = q[i]
    12. k,i=k+1,i+1
    13. else:
    14. res += mid - i + 1
    15. tep[k] = q[j]
    16. k,j=k+1,j+1
    17. while i<=mid:
    18. tep[k]=q[i]
    19. k,i=k+1,i+1
    20. while j<=r:
    21. tep[k]=q[j]
    22. k,j=k+1,j+1
    23. j = 0
    24. for i in range(l,r+1):
    25. q[i]=tep[j]
    26. j += 1
    27. return res
    28. print(merge_sort(a,0,n-1))

     四、第四题:小朋友排队

    解题思路:归并排序
            归并排序求出每个数与其他数组成的逆序对数,然后求和公式累加

    【Python程序代码】

    1. n = int(input())
    2. a = list(map(int, input().split()))
    3. q,tep = [],[[0]*2 for _ in range(n+5) ]
    4. for i in range(n):q.append( [a[i],i] )
    5. sum = [0]*(n+5)
    6. def merge(q,l,r):
    7. if l>=r:return
    8. mid = (l+r)>>1
    9. merge(q,l,mid);merge(q,mid+1,r)
    10. k,i,j=0,l,mid+1
    11. while i<=mid and j<=r:
    12. if q[i][0]<=q[j][0]:
    13. tep[k]=q[i]
    14. sum[q[i][1]] += j-mid-1
    15. k,i = k+1,i+1
    16. else:
    17. tep[k]=q[j]
    18. sum[q[j][1]] += mid-i+1
    19. k,j = k+1,j+1
    20. while i<=mid:
    21. tep[k] = q[i]
    22. sum[q[i][1]] += j-mid-1
    23. k,i = k+1,i+1
    24. while j<=r:
    25. tep[k] = q[j]
    26. k,j = k+1,j+1
    27. j = 0
    28. for i in range(l,r+1):
    29. q[i]=tep[j]
    30. j += 1
    31. merge(q,0,n-1)
    32. res = 0
    33. for i in range(n):
    34. res += (sum[i])*(sum[i]+1)//2
    35. print(res)

    五、第五题:超快速排序

    解题思路:归并排序
            归并排序求逆序对模板题

    【Python程序代码】

    1. import sys
    2. tep = [0] * (500005)
    3. def merge_sort(q,l,r):
    4. if l>=r:return 0
    5. mid = (l+r)//2
    6. res = merge_sort(q,l,mid) + merge_sort(q,mid+1,r)
    7. k,i,j = 0,l,mid+1
    8. while i<=mid and j<=r:
    9. if q[i]<=q[j]:
    10. tep[k]=q[i]
    11. k,i = k+1,i+1
    12. else:
    13. tep[k]=q[j]
    14. res += mid-i+1
    15. k,j = k+1,j+1
    16. while i<=mid:
    17. tep[k] = q[i]
    18. k,i = k+1,i+1
    19. while j<=r:
    20. tep[k] = q[j]
    21. k,j = k+1,j+1
    22. j = 0
    23. for i in range(l,r+1):
    24. q[i] = tep[j]
    25. j += 1
    26. return res
    27. n = int(sys.stdin.readline())
    28. while n!=0:
    29. a = []
    30. for i in range(n):
    31. a.append(int(sys.stdin.readline()))
    32. print(merge_sort(a,0,n-1))
    33. n = int(sys.stdin.readline())
  • 相关阅读:
    【FPGA零基础学习之旅#13】串口发送模块设计与验证
    C++ upper_bound()和lower_bound()(二分查找中使用)的定义,使用方法和区别
    机器学习笔记之线性分类——感知机算法
    带你进入桌面数控机床金工实训室
    【Python】模块与包的组织
    随机生成指定位数的字母数字组合的账号或密码
    UVa1311/LA2666 Servers
    微服务实践k8s&dapr开发部署实验(1)服务调用
    STM32移植SFUD
    Spring Boot 整合 Shiro(后端)
  • 原文地址:https://blog.csdn.net/w2563216521/article/details/136586550