Cod sursa(job #979983)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 3 august 2013 17:18:30
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <map>

using namespace std;

#define LeftSon(i) 2*i
#define RightSon(i) 2*i+1

#define Nmax 500005

struct nod
{
    int val;
    nod *left;
    nod *right;

} *BST;

int v[Nmax];
map<int,int> H;

int N;

ifstream f("algsort.in");
ofstream g("algsort.out");

void inserare( nod *&R, int key )
{
    if ( !R )
    {
        R = new nod;
        R->val = key;
        R->left = R->right = NULL;
    }
    else
        if ( R->val > key )
                inserare(R->left,key);
        else
                inserare(R->right,key);
}

void print( nod *R )
{
    if ( R  )
    {
        if ( R->left )
        print(R->left);

        int nr = H[R->val];

        while(nr)
            g << R->val << " ",
            nr--;

        H[R->val] = nr;

        if ( R->right )
        print(R->right);
    }
}

int main()
{
    int n, a;

    f >> n;
    for ( int i = 1; i <= n; i++ )
            f >> v[i],
            H[v[i]]++;

    random_shuffle( v + 1, v + n + 1 );
    random_shuffle( v + 1, v + n + 1 );
    random_shuffle( v + 1, v + n + 1 );
    random_shuffle( v + 1, v + n + 1 );

    for ( int i = 1; i <= n; i++ )
            inserare(BST,v[i]);

    print(BST);

    return 0;
}