Cod sursa(job #2286810)

Utilizator andrei.arnautuAndi Arnautu andrei.arnautu Data 20 noiembrie 2018 20:14:36
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
/**
  *  Worg
  */
#include <cstdio>
#include <algorithm>

#define DIM 500500
#define father(x) (x / 2)
#define left_son(x) (x * 2)
#define right_son(x) (x * 2 + 1)

using namespace std;

FILE *fin=freopen("algsort.in","r",stdin); FILE *fout=freopen("algsort.out","w",stdout);

int heap[DIM];
int n, nr;

inline void up(int nod)
{
  int key = heap[ nod ];

  while( nod > 1 && key > heap[ father(nod) ] )
  {
    heap[ nod ] = heap[ father(nod) ];
    nod = father( nod );
  }

  heap[ nod ] = key;
}

inline void down(int nod)
{
  int son = 1;

  while( son )
  {
    son = 0;

    if( left_son(nod) <= nr )
    {
      son = left_son( nod );
      if( right_son( nod ) <= nr && heap[ right_son(nod) ] > heap[ left_son(nod) ] )
        son = right_son( nod );
      if( heap[ son ] < heap[ nod ] )
        son = 0;
    }
    if( son )
    {
      swap( heap[son], heap[nod] );
      nod = son;
    }
  }
}

inline void Add(int x)
{
  heap[++nr] = x;
  up( nr );
}

int main()
{
  int i, x;
  scanf("%d", &n);
  for(i = 1; i <= n; ++i)
  {
    scanf("%d", &x);
    Add( x );
  }
  for(i = n; i > 1; --i)
  {
    swap( heap[1], heap[i] );
    --nr;
    down( 1 );
  }
  for(i = 1; i <= n; ++i)
    printf("%d ", heap[i]);
  return 0;
}