Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
865 B
0
Indexable
Never
import math
def merge(arr1, arr2, m, n):
    f_ptr = 0
    s_ptr = 0
    tmp = []
    while f_ptr < m and s_ptr < n:
        if arr1[f_ptr][1] >= arr2[s_ptr][1]:
            tmp.append(arr1[f_ptr])
            f_ptr += 1
        else:
            tmp.append(arr2[s_ptr])
            s_ptr += 1
    tmp += arr1[f_ptr:m] + arr2[s_ptr:n]
    return tmp

def merge_sort(arr):
    if len(arr) == 1:
        return arr
    print("Merging arr:", *[i[0] for i in arr])
    left = merge_sort(arr[:math.ceil(len(arr)/2)])
    right = merge_sort(arr[math.ceil(len(arr)/2):])
    return merge(left, right, len(left), len(right))

if __name__ == "__main__":
    n = int(input())
    arr = [0] + list(map(int, input().split())) + [0]
    arr = [(arr[i], max(arr[i-1], arr[i+1])) for i in range(1, len(arr)-1)]
    arr = merge_sort(arr)
    print("Answer:", *[i[0] for i in arr])