Untitled
unknown
plain_text
10 months ago
750 B
3
Indexable
Never
#python def heapify(arr, cnt): pair = [] flg = 0 for i in range(len(arr)-1, 0, -1): if arr[i] < arr[(i-1)//2]: flg = 1 if len(pair) == 0: pair = [i, (i-1)//2] elif (i-1)//2 == pair[1]: if arr[i] < arr[pair[0]]: pair[0] = i if flg: arr[pair[0]], arr[pair[1]] = arr[pair[1]], arr[pair[0]] cnt += 1 ans = heapify(arr, cnt) return [ans[0], ans[1], ans[2] + [[pair[1], pair[0]]]] else: return [arr, cnt, []] n = int(input()) arr = list(map(int, input().split())) ans = heapify(arr, 0) ans[2] = ans[2][::-1] print(ans[1]) print('\n'.join([" ".join(map(str, ans[2][i])) for i in range(len(ans[2]))]))
Leave a Comment