Problem:
Heap Sort
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($N^2$)
O($N^2$)
〇
〇
Insertion
Can be fast for an almost sorted array
Bubble Sort
ALDS1_2_A
O($N^2$)
O($N^2$)
〇
〇
Swap
Selection Sort
ALDS1_2_B
O($N^2$)
O($N^2$)
×
〇
Swap
Shell Sort
ALDS1_2_D
O($N^2$)
O($N log N$)
×
〇
Insertion
Merge Sort
ALDS1_5_A
O($N log N$)
O($N log N$)
〇
×
Divide and Conqure
Fast and stable.
It needs an external memory other than the input array.
Counting Sort
ALDS1_6_A
O($N + K$)
O($N + K$)
〇
×
Bucket
Fast and stable.
There is a limit to the value of array elements.
Quick Sort
ALDS1_6_B
O($N^2$)
O($N log N$)
×
△
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($N log N$)
O($N log N$)
×
〇
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 $A$ with $N$ 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 $N$ is given. In the next line, $A_i$ ($i=0,1,...,N−1$) are given separated by a single space character.
Output
Print $N$ integers of the permutation of $A$ separated by a single space character in a line.
This problem has multiple solutions and the judge will be performed by a special validator.
Constraints
$1 \leq N \leq 200000$
$0 \leq$ each element of $A$ $\leq 1000000000$
The elements of $A$ are all different
Sample Input and Output
Sample Input 1
8
1 2 3 5 9 12 15 23
Sample Output 1
23 9 15 2 5 3 12 1

Submitted solutions:

code_snippet: #include <stdio.h>
#include <stdlib.h>
#define gc() getchar()
#define pc(c) putchar(c)
#define p(i) i/2

void out(int a){
    char i = 0, str[10];
    do str[i++] = (a%10)|0x30, a /= 10; while(a > 0);
    while(i--) pc(str[i]);
}

int in(){
    int ret = 0, ch;
    while((ch = gc()) > ' ') ret = ret*10+(ch&0xf);
    return ret;
}

int cmp(const void* a, const void* b){
    return *(int*)a-*(int*)b;
}

int i, j, temp, n, A[200001];

int main(){
    n = in();
    for(i = 0; i < n; i++) A[i] = in();
    qsort(A, n, 4, cmp);
    A[n] = A[0];
    for(i = 1; i < n; i++){
        j = i;
        while(p(j) > 0 && A[j] > A[p(j)]){
            temp = A[j], A[j] = A[p(j)], A[p(j)] = temp;
            j = p(j);
        }
    }
    for(int i = 1; i < n; i++) out(A[i]), pc(' ');
    out(A[n]), pc('\n');
}
accuracy: 1
time_ms: 30
memory_kb: 4016
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int H;



void arrange(vector<int>& A)
{
    int i;
    int b = 0;
    int node = 1;
    int nextnode;

    for (i = H; i > 1; i /= 2) {
        b++;
    }
    while (b > 0) {
        if (H & (1 << b - 1)) {
            nextnode = 2*node+1;
        } else {
            nextnode = 2*node;
        }
        swap(A[node], A[nextnode]);
        node = nextnode;
        b--;
    }
}

int main()
{
    int i, n;
    cin >> H;
    n = H;
    vector<int> V(n);
    for (i = 0; i < n; i++) {
        cin >> V[i];
    }
    sort(V.begin(), V.end());

    vector<int> order(H+1);
    for (i = 1; i <= H; i++) {
        order[i] = i;
    }
    while (H > 1) {
        swap(order[1], order[H--]);
        arrange(order);
    }

    vector<int> A(n+1);
    for (i = 1; i <= n; i++) {
        A[order[i]] = V[i-1];
    }
        for (int i = 1; i <= n; i++) {
        if (i > 1) cout << " ";
        cout << A[i];
    }
    cout << endl;

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 5304
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
using namespace std;

void heapify(vector<int> &a, int i, int N) {
    int l = 2*i+1, r = 2*i+2;
    if (r < N) {
        if (l < N && a[l] < a[r]) l = r;
    }

    if (l < N && a[i] < a[l]) {
        swap(a[i], a[l]);
        heapify(a, l, N);
    }
}

void unheapify(vector<int> &a, int i, int N) {
    swap(a[0], a[i]);

    while (i) {
        int p = (i-1)/2;
        swap(a[i], a[p]);
        i = p;
    }
}

void heapSort(vector<int> &a, int N) {
    // 最大ヒープ
    for (int i = N/2-1; i >= 0; --i) {
        heapify(a, i, N);
    }

    for (int i = N-1; i >= 0; --i) {
        swap(a[0], a[i]);
        heapify(a, 0, i);
    }

    for (int i = 1; i < N-1; ++i) {
        unheapify(a, i, N);
    }
    swap(a[0], a[N-1]);

}

int main() {
    int N; cin >> N;
    vector<int> a(N);
    for (int i = 0; i < N; ++i) cin >> a[i];
    heapSort(a, N);

    for (int i = 0; i < N; ++i) {
        if (i == 0) cout << a[i];
        else cout << " " << a[i];
    }
    cout << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 4096
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#include <iomanip>

// input must be sorted
void build_my_heap(std::vector<int>& vec) {
    for (int i = 1; i < vec.size(); i++) {
        int now = i;
        std::swap(vec[now], vec[0]);
        int parent = (now - 1) / 2;
        while (vec[parent] < vec[now]) {
            std::swap(vec[parent], vec[now]);
            now = parent;
            parent = (now - 1) / 2;
        }
        if (i != vec.size() - 1) {
            now = i;
            parent = (now - 1) / 2;
            while (vec[parent] > vec[now]) {
                std::swap(vec[parent], vec[now]);
                now = parent;
                parent = (now - 1) / 2;
                if (now == 0) {
                    break;
                }
            }
        }
    }
}

int main() {
    int n;
    std::cin >> n;
    std::vector<int> vec(n);
    for (int i = 0; i < n; i++) std::cin >> vec[i];
    std::sort(vec.begin(), vec.end());
    build_my_heap(vec);
    for (int i = 0; i < vec.size(); i++) {
        if (i != 0) {
            std::cout << " ";
        }
        std::cout << vec[i];
    }
    std::cout << std::endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3780
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int MAX_N = 200000;
int heap[MAX_N+1];

void solve(int N) {
	sort(heap + 1, heap + N + 1);
	for (int n = 1; n <= N; ++n) {
		int i = n - 1, par = i / 2;
		while(par >= 1) {
			swap(heap[i], heap[par]);
			i = par, par = i/2;
		}
		swap(heap[1], heap[n]);
	}
}

int main() {
	int N;
	cin >> N;
	for (int i = 1; i <= N; ++i)
		cin >> heap[i];
	
	solve(N);
	
	for (int i = 1; i <= N; ++i) {
		cout << heap[i];
		if (i != N) cout << " ";
	}
	cout << endl;
	
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4124
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

int up(int i){
    return i / 2;
}

void buildMaxHeapAsInverseOfHeapSort(int A[], int N){
    sort(A+1, A+N+1, less<int>());
    for(int i = 2; i < N; i++){
        swap(A[1], A[i]);
        int j = i;
        while(j > 1){
            swap(A[j], A[up(j)]);
            j = up(j);
        }
    }
    swap(A[1], A[N]);
}

int main(){
    int N;
    cin >> N;
    int A[N + 1];
    for(int i = 1; i <= N; i++){
        cin >> A[i];
    }
    buildMaxHeapAsInverseOfHeapSort(A, N);
    for(int i = 1; i <= N; i++){
        cout << A[i];
        if(i <= N - 1){
            cout << " ";
        }else{
            cout << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3916
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

int up(int i){
    return i / 2;
}

void buildMaxHeap2(int A[], int N){
    sort(A+1, A+N+1, less<int>());
    for(int i = 2; i < N; i++){
        swap(A[1], A[i]);
        int j = i;
        while(j > 1){
            swap(A[j], A[up(j)]);
            j = up(j);
        }
    }
    swap(A[1], A[N]);
}

int main(){
    int N;
    cin >> N;
    int A[N + 1];
    for(int i = 1; i <= N; i++){
        cin >> A[i];
    }
    buildMaxHeap2(A, N);
    for(int i = 1; i <= N; i++){
        cout << A[i];
        if(i <= N - 1){
            cout << " ";
        }else{
            cout << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4124
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;

void maxheapify(int *a,int i,int n)
{
    int left=i*2;
    int right=i*2+1;
    int largest;
    if(left<=n&&a[left]>a[i])
    {
        largest=left;
    }
    else largest=i;
    if(right<=n&&a[right]>a[largest])
    {
        largest=right;
    }
    if(largest!=i)
    {
        swap(a[largest],a[i]);
        maxheapify(a,largest,n);
    }
}

void buildmaxheap(int *a,int n)
{
    for(int i=n/2;i>0;i--)
    {
        maxheapify(a,i,n);
    }
}

void heapsort(int *a,int n)
{
    for(int i=n;i>1;i--)
    {
        swap(a[1],a[i]);
        maxheapify(a,1,i-1);
    }
}

void update(int *a,int i)
{
    if(i>1)
    {
        if(a[i/2]>a[i])
        {
            swap(a[i/2],a[i]);
            update(a,i/2);
        }
    }
}

int main()
{
    int n;cin>>n;
    int a[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    buildmaxheap(a,n);
    heapsort(a,n);

    for(int i=2;i<=n;i++)
    {
        update(a,i-1);
        swap(a[1],a[i]);
    }

    for(int i=1;i<=n;i++)
    {
        if(i!=1) cout<<" ";
        cout<<a[i];
    }
    cout<<endl;
}
accuracy: 1
time_ms: 70
memory_kb: 3912
final_rank: 17

code_snippet: #include <algorithm>
#include <cstddef>
#include <iostream>
#include <utility>
#include <vector>
#include <ranges>
#include <cstdint>

// C++23では高速な入出力のため、std::ios_base::sync_with_stdio(false)や
// std::cin.tie(nullptr) を利用して、標準I/Oとの同期を解除します。

// 簡易な出力関数：余計なオーバーヘッドを避けるため、array_wrapper を排除し、
// 直接ループで出力します。
template <typename Container>
void print_container(const Container& c, std::string_view sep = " ") {
    if (c.empty()) return;
    auto it = std::cbegin(c);
    std::cout << *it;
    for (++it; it != std::cend(c); ++it) {
        std::cout << sep << *it;
    }
    std::cout << "\n";
}

// ヒープソートのworst-caseを作る関数。
// ※各swap操作はmaxHeapifyの25行目（疑似コード中の再帰呼び出し直前のswap）に対応しており
//    これが最大回数になるように構成しています。
template <typename T>
[[gnu::always_inline]] inline void make_worst_case_heap(std::vector<T>& vec) {
    const auto n = vec.size();
    if (n <= 1) [[unlikely]] return;
    // ループ中、vec[0]（ヒープの根）と各要素とをswapし、その後上方向にバブルアップさせる
    for (std::size_t i = 1; i + 1 < n; ++i) {
        std::swap(vec[0], vec[i]);
        std::size_t c = i;
        while (c > 0) {
            const std::size_t p = (c - 1) / 2;
            std::swap(vec[p], vec[c]);
            c = p;
        }
    }
    std::swap(vec[0], vec.back());
}

int main() {
    // I/O最適化：C++標準I/OとC標準I/Oの同期を解除し、tieも解除する
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::size_t N;
    std::cin >> N;
    std::vector<std::uint_fast32_t> vec;
    vec.resize(N);
    for (auto &x : vec) {
        std::cin >> x;
    }
    // まずソート（O(N log N)）し、最小値から最大値へ並ぶようにする
    std::ranges::sort(vec);
    // ヒープソートで最大ヒープ条件を満たしつつ、maxHeapifyのswap回数が最大となる順列に変換
    make_worst_case_heap(vec);
    // 出力：余計なオーバーヘッドを避けるため、シンプルなループで出力
    print_container(vec);
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4992
final_rank: 4

code_snippet: #include<iostream>
#include<vector>
#include<climits>
#include<algorithm>
using namespace std;

int main(){
  int n,j,k;
  cin >> n;
  int X[300000];
  for(int i=0;i<n;i++){
    cin >> X[i];
  }
  sort(X,X+n);
  for(int i=0;i<n-1;i++) {
    j=i;
    while(j>0){
      k=(j-1)/2;
      swap(X[k],X[j]);
      j=k;
    }
    swap(X[0],X[i+1]);
  }
  for(int i=0;i<n;i++){
    if(i!=0)cout<<" ";
    cout<<X[i];
  }
  cout << endl;

  
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4228
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
    int N; cin >> N;
    vector<int> A(N);
    for(int i = 0; i < N; i++) cin >> A[i];

    // 1. 配列を昇順にソート（最小→最大の順）
    sort(A.begin(), A.end());

    // 2. (i=0 .. N-2) 各要素を「根までバブルアップ」する
    for(int i = 0; i < N - 1; i++){
        int v = i;
        // 親のインデックスは (v - 1) / 2
        // ここでは「親が0番目以外なら swap する」という条件でループ
        while((v - 1) / 2){
            swap(A[v], A[(v - 1) / 2]);
            v = (v - 1) / 2;
        }
    }

    // 3. ルート(0番目)と末尾(N-1番目)を最後に1回交換
    swap(A[0], A[N - 1]);

    // 4. 出力
    for(int i = 0; i < N; i++){
        cout << A[i] << (i == N - 1 ? '\n' : ' ');
    }
}
accuracy: 1
time_ms: 60
memory_kb: 3760
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    int N; cin>>N;
    vector<int> A(N);
    for(int i=0;i<N;i++)cin>>A[i];
    sort(A.begin(),A.end());
    for(int i=0;i<N-1;i++){
        int v=i;
        while((v-1)/2){
            swap(A[v],A[(v-1)/2]);
            v=(v-1)/2;
        }
    }
    swap(A[0],A[N-1]);
    for(int i=0;i<N;i++)cout<<A[i]<<" \n"[i==N-1];
}
accuracy: 1
time_ms: 60
memory_kb: 3896
final_rank: 11

code_snippet: #include<iostream>
#include<utility>
#include<algorithm>
#include<vector>
using namespace std;

int main(){
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    int n;
    cin>>n;
    vector<int> A(n);
    for(auto &e:A) cin>>e;
    sort(A.begin(),A.end());
    for(int i=0;i<n-1;i++){
        int val=i;
        while((val-1)/2){
            int nev=(val-1)/2;
            swap(A[val],A[nev]);
            val=nev;
        }
    }
    swap(A.front(),A.back());
    for(int i=0;i<n;i++) cout<<A[i]<<" \n"[i+1==n];
}
accuracy: 1
time_ms: 30
memory_kb: 3692
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Heap {
  int heap_size, N;
  vector<int> A;
  Heap(int N) : heap_size(N), N(N), A(N + 1) {}
  int parent(int i) { return floor(i / 2); }
  int left(int i) { return i * 2; }
  int right(int i) { return left(i) + 1; }
  void maxHeapify(int i) {
    int l = left(i), r = right(i), largest;
    largest = l <= heap_size && A[l] > A[i] ? l : i;
    largest = r <= heap_size && A[r] > A[largest] ? r : largest;
    if (largest != i) {
      swap(A[i], A[largest]);
      maxHeapify(largest);
    }
  }
  void minHeapify(int i) {
    int p = parent(i);
    if (p > 0) {
      swap(A[i], A[p]);
      minHeapify(p);
    }
  }
  void buildMaxHeap() {
    for (int i = N / 2; 0 < i; i--) {
      maxHeapify(i);
    }
  }
  void heapSort() {
    buildMaxHeap();
    while (heap_size >= 2) {
      swap(A[1], A[heap_size]);
      heap_size--;
      maxHeapify(1);
    }
  }
  void reverseHeapSort() {
    heap_size = 1;
    while (heap_size < N) {
      minHeapify(heap_size);
      heap_size++;
      swap(A[1], A[heap_size]);
    }
  }
};

int main() {
  int H;
  cin >> H;
  Heap heap(H);
  for (int i = 1; i <= H; i++) {
    cin >> heap.A[i];
  }
  heap.heapSort();
  heap.reverseHeapSort();
  for (int i = 1; i <= H; i++) {
    cout << heap.A[i] << (i == H ? '\n' : ' ');
  }
}
accuracy: 1
time_ms: 60
memory_kb: 3864
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


void swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

// 0
// 1   2
// 3 4 5 6
// 7...
// のイメージで
size_t get_parent_idx(size_t current_idx) {
    return (current_idx + 1) / 2 - 1;
}

size_t get_left_idx(size_t current_idx) {
    return 2 * (current_idx + 1) - 1;
}

size_t get_right_idx(size_t current_idx) {
    return 2 * (current_idx + 1) -1 + 1;
}

// 常にroot_idxの要素がヒープの最後の要素(heap_end_idx位置)になるように要素を入れ替える関数
void fake_max_heapify(vector<int>& heap, size_t heap_end_idx) {
    // 末尾から入れ替えられる要素をたどって記録
    vector<int> indexes_on_route;
    size_t current_idx = heap_end_idx;
    indexes_on_route.push_back(heap_end_idx);
    // 末尾の要素から親をたどる
    while (current_idx != 0) {
        auto parent = get_parent_idx(current_idx);
        indexes_on_route.push_back(parent);
        current_idx = parent;
    }

    // root_idxが末尾と入れ替わるようになってほしいので、列挙した親と順に入れ替わる動きをシミュレーションする
    // そのような操作になる場合を探したいだけなので、root_idxと親要素との入れ替えが常に最大ヒープ条件を
    // 満たしていることにする
    for (auto i = 0; i < indexes_on_route.size() - 1; i++) {
        ::swap(heap[indexes_on_route[indexes_on_route.size()-1-(i+1)]], heap[indexes_on_route[indexes_on_route.size()-1-i]]);
    }
}

int main() {
    int n;
    vector<long long int> values;
    vector<int> indexes;

    cin >> n;
    for (auto i = 0; i < n; i++) {
        long long int a;
        cin >> a;
        values.push_back(a);
        indexes.push_back(i);
    }

    sort(values.begin(), values.end());

    // ヒープの最後の要素が最小の値である場合をシミュレーションして
    // ヒープから抽出される要素の添え字を出てくる順に並べる
    int heap_size = n;
    while (heap_size > 0) {
        // 既にヒープ条件を満たすので最初の要素をpopし、末尾と入れ替える
        ::swap(indexes[0], indexes[heap_size-1]);
        heap_size--;

        // 今根にいる要素がmax_heapifyで末尾に移動することをシミュレーションする
        if (heap_size > 1) {
            fake_max_heapify(indexes, heap_size - 1);
        }
    }

    // indexesで並んでいる順に要素がpopされている
    // -> valuesをindexesにある添え字の順番に並べ替えれば、valuesの値を持つヒープであって、
    //    シミュレーションした操作が行われるものの例を構築できる
    long long int res[n];
    for (auto i = 0; i < indexes.size(); i++) {
        res[indexes[i]] = values[i];
    }
    for (auto i = 0; i < n; i++) {
        cout << res[i];
        if (i < n - 1) {
            cout << " ";
        }
    }
    cout << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 7756
final_rank: 20

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<deque>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;

const ld EPS = 1e-10;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
const ld PI = acos( -1.0 );

#define SWAP( A, B ) { auto tmp = A; A = B; B = tmp; }

template <typename T>
void print( vector<T> v ){
    if( v.size() == 0 ) return;
    for( int i = 0 ; i < v.size() - 1 ; i++ )
        cout << v[i] << ' ';
    cout << v[v.size()-1] << endl;
}

void min_heapify( vector<ll> &v, int id ){

    while( true ){
        int parent = ( id - 1 ) / 2;
        if( parent < 0 ) break;
        if( v[parent] > v[id] ){
            SWAP( v[parent], v[id] );
        }
        else
            break;
        id = parent;
    }

}

int main(){

    int n;
    ll key;
    vector<ll> res;

    cin >> n;
    for( int i = 0 ; i < n ; i++ ){
        cin >> key;
        res.push_back( key );
    }
    sort( res.begin(), res.end() );
    for( int i = 1 ; i < n - 1 ; i++ ){
        SWAP( res[0], res[i] );
        min_heapify( res, i );
    }
    SWAP( res[0], res[n-1] );

    print( res );

}
accuracy: 1
time_ms: 60
memory_kb: 6000
final_rank: 19

code_snippet: // Heap Sort

#include <iostream>
#include <vector>
#include <algorithm>

void unsift(std::vector<int> *A, int i, int bound);
void reverseSorting(std::vector<int> *A);

int main()
{
    int N;
    std::cin >> N;
    std::vector<int> A(N + 1, 0);
    for (size_t i = 0; i < N; i++)
    {
        int a;
        std::cin >> a;
        A[i + 1] = a;
    }

    reverseSorting(&A);

    for (size_t i = 0; i < N; i++)
    {
        if (i != 0)
            std::cout << " ";
        std::cout << A[i + 1];
    }
    std::cout << std::endl;
    return 0;
}

// 今回の用途ではboundは1で固定なので引数としては本当は無用
void unsift(std::vector<int> *A, int i, int bound)
{
    int parent = i / 2;
    if (parent >= bound)
    {
        // std::cout << " swap: " << (*A)[i] << "<->" << (*A)[parent] << std::endl;
        // 無条件で交換していく
        std::swap((*A)[i], (*A)[parent]);
        unsift(A, parent, bound);
    }
}

void reverseSorting(std::vector<int> *A)
{
    std::sort(A->begin() + 1, A->end());
    int N = A->size() - 1;

    // iをヒープ領域の終端とする
    for (size_t i = 1; i < N; i++)
    {
        // 常にヒープの最後尾が最小値
        // ヒープ範囲内の最小値を先頭へアンシフトする
        unsift(A, i, 1);
        // 先頭に来た最小値とヒープの最後尾の1つ後ろを交換
        std::swap((*A)[1], (*A)[i + 1]);
    }
}
accuracy: 1
time_ms: 60
memory_kb: 3860
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <string.h>

using namespace std;


void maxHeapify(vector<int> &A, int i, int size) {
    int largestIndex = i;

    int leftChildIndex = 2 * i + 1;
    int rightChildIndex = 2 * i + 2;
    if (size > leftChildIndex && A[leftChildIndex] > A[largestIndex]) largestIndex = leftChildIndex;;
    if (size > rightChildIndex && A[rightChildIndex] > A[largestIndex]) largestIndex = rightChildIndex;
      
    if (largestIndex != i) {
         swap(A[i], A[largestIndex]);
         maxHeapify(A, largestIndex, size);
    }
}

void heapSort(vector<int> &A) {
    int size = A.size();
    for (int i = (A.size() - 1) / 2; i >= 0; i--) {
        maxHeapify(A, i, size);
    }
    while (true) {
        swap(A[0], A[size - 1]);
        size--;
        if (size < 2) break;
        maxHeapify(A, 0, size);
    }

}

int main() {
    int n;
    vector<int> A;
    
    cin >> n;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        A.push_back(a);
    }

    //heapSort(A);
    sort(A.begin(), A.end());

    
    for (int i = 1; i < n-1; i++) {
        swap(A[0], A[i]);

        int index = i;
        while (index > 0) {
            swap(A[index], A[(index-1) / 2]);
            index = (index-1) / 2;
        }
    }
    swap(A[0], A.back());

    for (int i = 0; i < n; i++) {
        cout << A[i];
        if (i < n - 1) {
            cout << " ";
        }
        else {
            cout << endl;
        }
    }
    




    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4084
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
#else
  #define Debug(...) void(0)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1, -1e9);
  rep(i, n) cin >> a[i + 1];
  sort(a.begin(), a.end());

  for (int i = 1; i < n; ++i) {
    for (int j = i; j > 1; j /= 2) swap(a[j], a[j / 2]);
    swap(a[i + 1], a[1]);
  }

  rep(i, n) cout << a[i + 1] << " \n"[i == n - 1];
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3736
final_rank: 6

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;


void maxheap_swap(vector<int> &v,int p)
{
    int cur=p;
    int parent=p/2;
    while(parent>1)
    {
        swap(v[cur],v[parent]);
        cur=parent;
        parent=cur/2;
    }
    
}
int main() 
{
    vector<int>b;
    int h;
    scanf("%d",&h);
    b.resize(h+1);
    for(int i=1;i<=h;i++)scanf("%d",&b[i]);

    sort(b.begin()+1,b.end());
    
    for(int i=1;i<h;i++)maxheap_swap(b,i);
    swap(b[1],b[h]);
    
    for(int i=1;i<h;i++)printf("%d ",b[i]);
    printf("%d\n",b[h]);

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3624
final_rank: 1