原题链接:
题解:
套板子即可
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int h[N], s;
void down(int x) {
int t = x;
if (2 * x <= s && h[2 * x] < h[x]) t = 2 * x;
if (2 * x + 1 <= s && h[2 * x + 1] < h[t]) t = 2 * x + 1;
if (t != x) {
swap(h[x], h[t]);
down(t);
}
}
int main() {
int n, m;cin >> n >> m;
s = n;
for (int i = 1;i <= n;i++) cin >> h[i];
for (int i = n / 2;i;i--) down(i);
while (m--) {
cout << h[1] << " ";
swap(h[1], h[s--]);
down(1);
}
}