#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;
}
相关文章
- Heap Allocation Profiles (heap=sites)
- 关于java.lang.OutOfMemoryError: Java heap space的错误分析
- 选择排序算法:堆排序-Heap Sort
- webpack起服务器报JavaScript heap out of memory
- 内存不足导致的java.lang.OutOfMemoryError: java heap space引出java工程启动参数设置
- JVM内存参数详解以及配置调优(二)-Heap参数
- android Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine.
- 应用jacob组件造成的内存溢出解决方案(java.lang.OutOfMemoryError: Java heap space)
- The Usage of Lambda and Heap in the C++ STL
- An internal error has occurred. Java heap space