#include
using namespace std;
int heap[100010],cnt=0;
void put(int x)
{
cnt++;
heap[cnt]=x;
int now=cnt;
int next=cnt;
while(now>1)
{
next=now/2;
if(heap[next]<=heap[now])break;
swap(heap[next],heap[now]);
now=next;
}
}
int get()
{
int res=heap[1];
heap[1]=heap[cnt];
cnt--;
int now=1;
int next=1;
while(now*2<=cnt)
{
next=now*2;
if(heap[next]>heap[next+1]&&next<=cnt)next++;
if(heap[next]>=heap[now])break;
swap(heap[next],heap[now]);
now=next;
}
return res;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
put(x);
}
for(int i=1;i<=n;i++)
{
cout<<get()<<' ';
}
cout<<endl;
return 0;
}
相关文章
- C++ STL 堆(heap)的初始化及其正确使用
- algorithm: heap sort in python 算法导论 堆排序
- npm异常:FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed
- 三步搞定OOM内存溢出,记一次使用Arthas处理OOM内存溢出问题: Java heap space
- StringBuilder 导致堆内存溢出 Java heap space /GC overhead limit exceeded
- Java jmap与jcmd命令dump内存heap堆
- kafka消费报错卡死:内存溢出OutOfMemoryError: Java heap space
- Error occurred during initialization of VM Could not reserve enough space for 786432KB object heap
- Error occurred during initialization of VM Could not reserve enough space for 1572864KB object heap
- Error occurred during initialization of VM Could not reserve enough space for 1048576KB object heap