Cod sursa(job #980058)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 3 august 2013 20:59:48
Problema Sortare prin comparare Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 3.28 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <list>

using namespace std;

#define Hmax 500005

int lg2( int key )
{
    int lg = -1;

    while( key )
    {
        lg++;
        key >>= 1;
    }

    return lg;
}

struct nod
{
    int val;
    int ind;
};

struct cmp {

    bool operator() (nod &a, nod &b) {
        return a.val > b.val;
    }
};

struct NODE
{
    list<int> l;

} lista[Hmax];

void AlexSort( int *v, int N )
{
    int log2 = lg2( N );
    int NR = ( N % log2 == 0 ? N / log2 : N / log2 + 1 );

    int ind = 1;

    for ( int i = 1; i <= NR; ++i )
    {
        for ( int j = 0; j < log2; ++j )
        {
            if ( ind > N )
                    break;

            lista[i].l.push_back ( v[ ind++ ]);
        }
    }

    for ( int i = 1; i <= NR; ++i )
    {
        lista[i].l.sort();
    }

    priority_queue <nod, vector <nod>, cmp> heap;


    for ( int i = 1; i <= NR; i++ )
    {
        nod x;
        x.val = lista[i].l.front();
        x.ind = i;

        lista[i].l.pop_front();

        heap.push( x );
    }

    int index = 1;

    while( !heap.empty() )
    {
        v[ index++ ] = heap.top().val;
        ind = heap.top().ind;
        heap.pop();

        if ( lista[ind].l.size() > 0 )
        {
            nod x;
            x.val = lista[ind].l.front();
            x.ind = ind;

            lista[ind].l.pop_front();

            heap.push( x );
        }
    }
}

int main()
{
    int a[Hmax], n;

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

    f >> n;
    for ( int i = 1; i <= n; i++ )
            f >> a[i];

    AlexSort(a,n);

    for ( int i = 1; i <= n; i++ )
            g<<a[i]<<" ";

    return 0;
}



/*
class MinHeap
{
    public:

        MinHeap();
       ~MinHeap(){};

        void push( int key );
        void pop( );
        inline int top( );
        inline int size( );
        inline bool empty( );


    private:

        int H[Hmax];

        int N;

        inline int Parent( int i ){ return i/2; };
        inline int LeftSon( int i ){ return 2*i; };
        inline int RightSon( int i ){ return 2*i+1; };

        void DownHeap( int poz );
        void UpHeap( int poz );
};

MinHeap::MinHeap()
{
    for ( int i = 0; i < Hmax; ++i )
            H[i] = 0;

    N = 0;
}

void MinHeap::DownHeap( int poz )
{
    int k = poz;
    int j;

    do
    {
        j = k;

        if ( LeftSon(j) <= N && H[LeftSon(j)] < H[k] )   k = LeftSon(j);
        if ( RightSon(j) <= N && H[RightSon(j)] < H[k] ) k = RightSon(j);

        swap( H[j], H[k] );

    } while( j != k );
}

void MinHeap::UpHeap( int poz )
{
    int k = poz;
    int j;

    do
    {
        j = k;

        if ( j > 1 && H[Parent(j)] > H[k] ) k = Parent(j);

        swap( H[j], H[k] );

    } while( j != k );
}

void MinHeap::push( int key )
{
    H[ ++N ] = key;

    UpHeap( N );
}

void MinHeap::pop( )
{
    H[1] = H[N];

    DownHeap( 1 );

    N--;
}

inline int MinHeap::top( )
{
    return H[1];
}

inline int MinHeap::size( )
{
    return N;
}

inline bool MinHeap::empty( )
{
    return ( N == 0 );
}
*/