python用归并后面超时

P1908 逆序对

Happinessa @ 2024-12-16 14:38:44

各位大佬我写的有问题嘛

import sys

def MergeSort(data, l, r):
    if l >= r:
        return
    mid = l + r >> 1
    MergeSort(data, l, mid)
    MergeSort(data, mid + 1, r)
    global cnt
    t = []
    i, j = l, mid + 1
    while i <= mid and j <= r:
        if data[i] <= data[j]:
            t.append(data[i])
            i += 1
        else:
            t.append(data[j])
            cnt += mid - i + 1
            j += 1
    while i <= mid:
        t.append(data[i])
        i += 1
    while j <= r:
        t.append(data[j])
        j += 1
    data[l : l + len(t)] = t

n = int(sys.stdin.readline().strip("\n"))
# n, m = map(int, sys.stdin.readline().strip().split(" "))
a = list(map(int, sys.stdin.readline().strip("\n").split(" ")))
cnt = 0
MergeSort(a, 0, len(a) - 1)
print(cnt)

by mayisang @ 2024-12-21 18:46:18

python是这样的


|