Pagini recente » Cod sursa (job #2749429) | Cod sursa (job #2795284) | Cod sursa (job #2091073) | Cod sursa (job #1250307) | Cod sursa (job #1846199)
#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]--;
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{x} );
}
for(int i=1; i<=n; i++)
{
cout<<t.top().lengh<<" ";
t.pop();
}
return 0;
}