Cod sursa(job #1846202)

Utilizator ASD135Radu M ASD135 Data 12 ianuarie 2017 12:43:03
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.43 kb
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;


///prioriry_queue

struct tipe
{
	  int lengh;
};

const int Q=500007;

 int stv[Q];
tipe prel[Q];

void preproc()
{
	for(int i=Q-1; i>0; i--)
	{
		stv[++stv[0]]=i;
	}
}


struct heap
{
	 int h[Q];

	void swap( int p,  int p2)
	{
		int aux=h[p];
		h[p]=h[p2];
		h[p2]=aux;
	}

	void urca( int p)
	{
		while(p!=1 && prel[h[p]].lengh<prel[h[p/2]].lengh)
		{
			swap(p,p/2);
			p/=2;
		}
	}
	void coboara( int p)
	{
		int p0;
		while(true)
		{
			p0=p;

			if(2*p<=h[0] && prel[h[2*p]].lengh<prel[h[p0]].lengh)
				p0=2*p;
			if(2*p+1<=h[0] && prel[h[2*p+1]].lengh < prel[h[p0]].lengh)
				p0=2*p+1;

			if(p==p0)
				break;
			swap(p,p0);
			p=p0;
		}
	}

	int size()
	{
		return h[0];
	}

	void push(const tipe &x)
	{
		int loc;
		loc=stv[stv[0]];
		stv[0]--;
		prel[loc].lengh=x.lengh;
		h[++h[0]]=loc;
		urca(h[0]);
	}

	tipe top()
	{
		return prel[h[1]];
	}
	void pop()
	{
		stv[++stv[0]]=h[1];
		swap(1,h[0]);
		h[0]--;
		coboara(1);
	}

} t;


///parsare
 
bool digit[200];
 
 
const int SBUF=4096;
char BUF[SBUF];
int pbuf=SBUF;
 
char get_char()
{
    if(pbuf==SBUF)
    {
        pbuf=0;
        fread(BUF,SBUF,1,stdin);
    }
    return BUF[pbuf++];
}
 
char x;
 
int get_int()
{
    x=get_char();
 
    while(digit[x]==0)
        x=get_char();
 
    int rez=0;
 
    while(digit[x])
    {
        rez=rez*10+x-'0';
        x=get_char();
    }
 
    return rez;
}
 
int zece[8];
 
double get_double()
{
    x=get_char();
 
    while(digit[x]==0)
        x=get_char();
 
    int rez=0;
 
    while(digit[x])
    {
        rez=rez*10+x-'0';
        x=get_char();
    }
    if(x=='.')
    {
        int rez2=0;
        x=get_char();
 
        int cont=0;
 
        while(digit[x])
        {
            rez2=rez2*10+x-'0';
            cont++;
            x=get_char();
        }
 
        double fin=rez2;
        fin/=zece[cont];
        fin+=rez;
        return fin;
    }
    else
        return rez;
}
 
void jibril()
{
    zece[0]=1;
    for(int i=1; i<=8; i++)
    {
        zece[i]=zece[i-1]*10;
    }
 
    for(int i='0'; i<='9'; i++)
    {
        digit[i]=1;
    }
}
 
///parsare



int main()
{
	freopen("algsort.in","r",stdin);
	freopen("algsort.out","w",stdout);

	jibril();
	preproc();

	int n,x;

	//cin>>n;
	n=get_int();

	for(int i=1; i<=n; i++)
	{
	//	cin>>x;
		x=get_int();
		t.push(tipe{x} );
	}

	for(int i=1; i<=n; i++)
	{
		cout<<t.top().lengh<<" ";
		t.pop();
	}

    return 0;
}