1 #include <algorithm> 2 #include <cstdio> 3 4 using namespace std; 5 6 int x,size,n; 7 int heap[1000005]; 8 9 void push(int x) 10 { 11 int now,next; 12 heap[++size]=x; 13 now=size; 14 while(now>1) 15 { 16 next=now/2; 17 if(heap[next]<=heap[now]) return ; 18 swap(heap[next],heap[now]); 19 now=next; 20 } 21 } 22 23 int pop() 24 { 25 int now,next; 26 int ret=heap[1]; 27 heap[1]=heap[size--]; 28 now=1; 29 while(now*2<=size) 30 { 31 next=now*2; 32 if(next<size&&heap[next]>heap[next+1]) next++; 33 if(heap[now]<=heap[next]) return ret; 34 swap(heap[now],heap[next]); 35 now=next; 36 } 37 return ret; 38 } 39 40 int main() 41 { 42 scanf("%d",&n); 43 for(int i=1;i<=n;i++) 44 { 45 scanf("%d",&x); 46 push(x); 47 } 48 for(int i=1;i<n;i++) 49 { 50 int ans=pop(); 51 printf("%d ",ans); 52 } 53 printf("%d\n",pop()); 54 return 0; 55 }
时间: 2024-10-12 13:46:25