Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import heapq
- invs = 0
- def Merge(arr1, arr2):
- global invs
- mArr = []
- L1, L2 = 0, 0
- while len(arr1[L1:]) > 0 and len(arr2[L2:]) > 0:
- if arr1[L1] > arr2[L2]:
- invs += len(arr1[L1:])
- mArr.append(arr2[L2])
- L2 += 1
- else:
- mArr.append(arr1[L1])
- L1 += 1
- if len(arr1[L1:]) > 0:
- mArr += arr1[L1:]
- elif len(arr2[L2:]) > 0:
- mArr += arr2[L2:]
- return mArr
- def MergeSort(arr):
- h = []
- for i in arr:
- heapq.heappush(h, [i])
- while len(h) > 1:
- mrg = Merge(heapq.heappop(h), heapq.heappop(h))
- heapq.heappush(h, mrg)
- return heapq.heappop(h)
- def main():
- global invs
- n = int(input())
- arr = [int(i) for i in input().split()]
- MergeSort(arr)
- print(invs)
- if __name__ == "__main__":
- main()
- """при входных данных:
- 5
- 2 3 9 2 9
- Переменная invs = 0, хотя должно быть: invs = 2"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement