AcWing 838. 堆排序
AcWing 838. 堆排序
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int h[N],num;
void down(int u){
int t=u;
if(u*2<=num&&h[u*2]<h[t]) t=u*2;
if(u*2+1<=num&&h[u*2+1]<h[t]) t=u*2+1;
if(u!=t){
swap(h[u],h[t]);
down(t);
}
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&h[i]);
num=n;
for(int i=n/2;i>0;i--) down(i);
while(m--){
printf("%d ",h[1]);
h[1]=h[num];
num--;
down(1);
}
return 0;
}
AcWing 838. 堆排序
原文地址:https://www.cnblogs.com/wiseXu/p/13403073.html