Problem 2700 --9_D : Heap Sort

2700: 9_D : Heap Sort

"
Time Limit $1$ 秒/Second(s) Memory Limit $512$ 兆字节/Megabyte(s)
提交总数 $0$ 正确数量 $0$
裁判形式 标准裁判/Standard Judge 我的状态 尚未尝试
难度 分类标签 STL

There are a number of sorting algorithms which have different characteristics as follows.

Algorithm Time Complexity
(Worst)
Time Complexity
(Average)
Stability Memory
Efficiency
Method Features
Insertion Sort
ALDS1_1_A
O(N2N2) O(N2N2) Insertion Can be fast for an almost sorted array
Bubble Sort
ALDS1_2_A
O(N2N2) O(N2N2) Swap
Selection Sort
ALDS1_2_B
O(N2N2) O(N2N2) × Swap
Shell Sort
ALDS1_2_D
O(N2N2) O(NlogNNlogN) × Insertion
Merge Sort
ALDS1_5_A
O(NlogNNlogN) O(NlogNNlogN) × Divide and Conqure Fast and stable.
It needs an external memory other than the input array.
Counting Sort
ALDS1_6_A
O(N+KN+K) O(N+KN+K) × Bucket Fast and stable.
There is a limit to the value of array elements.
Quick Sort
ALDS1_6_B
O(N2N2) O(NlogNNlogN) × Divide and Conqure Fast and almost-in-place if it takes a measure for the corner cases. Naive implementation can be slow for the corner cases.
Heap Sort
ALDS1_9_D (This problem)
O(NlogNNlogN) O(NlogNNlogN) × Heap structure Fast and in-place. It is unstable and performs random access for non-continuous elements frequently.

The Heap Sort algorithms is one of fast algorithms which is based on the heap data structure. The algorithms can be implemented as follows.

1  maxHeapify(A, i)
2      l = left(i)
3      r = right(i)
4      // select the node which has the maximum value
5      if l ≤ heapSize and A[l] > A[i]
6          largest = l
7      else 
8          largest = i
9      if r ≤ heapSize and A[r] > A[largest]
10         largest = r
11
12     if largest ≠ i 
13         swap A[i] and A[largest]
14         maxHeapify(A, largest) 
15
16 heapSort(A):
17     // buildMaxHeap
18     for i = N/2 downto 1:
19         maxHeapify(A, i)
20     // sort
21     heapSize ← N
22     while heapSize ≥ 2:
23         swap(A[1], A[heapSize])
24         heapSize--
25         maxHeapify(A, 1)

On the other hand, the heap sort algorithm exchanges non-continuous elements through random accesses frequently.

Your task is to find a permutation of a given sequence AA with NN elements, such that it is a max-heap and when converting it to a sorted sequence, the total number of swaps in maxHeapify of line 25 in the pseudo code is maximal possible.


Input

In the first line, an integer NN is given. In the next line, AiAi (i=0,1,...,N1i=0,1,...,N−1) are given separated by a single space character.


Output

Print NN integers of the permutation of AA separated by a single space character in a line.

This problem has multiple solutions and the judge will be performed by a special validator.


8
1 2 3 5 9 12 15 23
23 9 15 2 5 3 12 1

Constraints

  • 1N2000001≤N≤200000
  • 00≤ each element of AA 1000000000≤1000000000
  • The elements of AA are all different


推荐代码 查看2700 所有题解 上传题解视频得图灵币

本题记录 用 户(点击查看用户) 运行号(点击购买题解) 时 间
算法最快[$ $ms]
内存最少[$ $KB]
第一AC
第一挑战

赛题来源/所属竞赛 会津大学《挑战数据结构与算法》 挑战数据结构与算法

竞赛编号 竞赛名称 竞赛时间 访问比赛