Pagini recente » Cod sursa (job #139731) | Cod sursa (job #911617) | Cod sursa (job #1310145) | Cod sursa (job #1449165) | Cod sursa (job #752162)
Cod sursa(job #752162)
#include <cstdio>
#include <algorithm>
using namespace std;
int n,h[500005],k;
void push(int x){
h[++k] = x;
for(int t=k; t/2>0 && h[t/2] > h[t] ;t/=2) swap(h[t/2],h[t]);
}
void pop(){
int f,t=1;
h[1] = h[k--];
if(h[2] < h[3]) f = 2; else f = 3;
// for(int t=1; f <= k && h[t] > h[f] ; h[2*t] > h[2*t+1] ? f = 2*t : f = 2*t+1 , t=f) swap(h[t],h[f]);
while( f <= k && h[t] > h[f] )
{
swap(h[f],h[t]);
t = f;
f = 2*t;
if( f + 1 <= k && h[f + 1] < h[f]) f++;
}
}
int main(){
int x;
freopen("algsort.in","r",stdin);
freopen("algsort.out","w",stdout);
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&x);
push(x);
}
for(int i=0;i<n;i++)
{
printf("%d ",h[1]);
pop();
}
return 0;
}