Cod sursa(job #752155)

Utilizator test0Victor test0 Data 27 mai 2012 22:13:43
Problema Sortare prin comparare Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#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(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;
}