堆排序
#include<iostream>
using namespace std;
const int N = 100010;
int h[N];
int r;//右边界
int n, m;
void down(int u) {
int t = u;//t指的是最小的数所在的下标
if (u * 2 <= r && h[t] > h[u * 2]) t = u * 2; //左孩子
if (u * 2 + 1 <= r && h[t] > h[u * 2 + 1]) t = u * 2 + 1;//右孩子
if (t != u) {
swap(h[t], h[u]);//把最小的换上来
down(t);
}
}
int main() {
cin >> n >> m;
r = n;
for (int i = 1; i <= n; i ++ ) cin >> h[i];
for (int i = n / 2; i >= 1; i -- ) down(i);
while (m -- ) {
cout << h[1] << " ";
swap(h[1], h[r]);
r -- ;
down(1);
}
}