请看完后点个赞,谢谢
//堆模版 22-03-27
#include <bits/stdc++.h>
using namespace std;
int heap[11]={0,1,1,2,5,4,4,3,7,6} ;
int heapsz=0;
void push(int n)//维护大顶堆
{
int now,next;//指向当前与下一个节点
heapsz++;heap[heapsz]=n;now=heapsz;
while (now>1)
{
next=now/2;
if (heap[now]>=heap[next]) swap(heap[now],heap[next]);
else break;
now=next;
}
}
void print()
{
for (int i=1;i<=5;i++) cout<<heap[i]<<" ";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
//建堆
for (int i=1;i<=10;i++)
{
int tmp;cin>>tmp;
push(tmp);print();
}
}
https://blog.youkuaiyun.com/zeekliu/article/details/124098799