Cod sursa(job #1846198)

Utilizator ASD135Radu M ASD135 Data 12 ianuarie 2017 12:38:47
Problema Sortare prin comparare Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;


///prioriry_queue

struct tipe
{
	  int a;
	  int lengh;
	  int bg;
	  int vin;
};

const int Q=1000001;

 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].a=x.a;
		prel[loc].bg=x.bg;
		prel[loc].lengh=x.lengh;
		prel[loc].vin=x.vin;

		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;




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

	preproc();

	int n,x;

	cin>>n;

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

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

    return 0;
}