Created
April 7, 2016 23:20
-
-
Save zigzackey/8687a64f1bd6a39ec742ae52d31dd931 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def merge(A, left, mid, right): | |
global cnt | |
L = A[left:mid] + [1000000001] | |
R = A[mid:right] + [1000000001] | |
i = j = 0 | |
for k in range(left, right): | |
if L[i] <= R[j]: | |
A[k] = L[i] | |
i += 1 | |
else: | |
A[k] = R[j] | |
j += 1 | |
cnt += right - left | |
def mergeSort(A, left, right): | |
if left + 1 < right: | |
mid = (left + right) // 2 | |
mergeSort(A, left, mid) | |
mergeSort(A, mid, right) | |
merge(A, left, mid, right) | |
if __name__ == '__main__': | |
n = int(input()) | |
A = list(map(int, input().split())) | |
cnt = 0 | |
mergeSort(A, 0, n) | |
print(*A) | |
print(cnt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment