#include <bits/stdc++.h>
using namespace std;
int n, tot = n;
int a[200010], b[100010];
void adjust(int i) {
int k;
if (i > n)
return ;
if (a[i * 2] > a[i * 2 + 1]) {
k = i * 2;
} else
k = i * 2 + 1;
if (a[i] > a[k])
return ;
else {
swap(a[i], a[k]);
adjust(k);
}
}
deque<int>q;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = n / 2; i >= 1; i--) {
adjust(i);
}
for (int i = 1; i <= n; i++) {
q.push_back(a[1]);
swap(a[tot], a[1]);
tot--;
adjust(1);
}
while (!q.empty()) {
printf("%d ", q.back());
q.pop_back();
}
return 0;
}
我的tot变量初始值为0,我那里tot--,又调用a[tot],它不会数组越界吗