Pagini recente » Cod sursa (job #3351994) | Monitorul de evaluare | Cod sursa (job #1324463) | Cod sursa (job #1129773) | Cod sursa (job #2270103)
#include <bits/stdc++.h>
using namespace std;
const int NMAX=(5*(1e5)+5);
int H[NMAX], N, i, A[NMAX], Length;
void HeapUp (int X)
{
if(X>1)
{
if(H[X]>H[X/2])
{
swap(H[X], H[X/2]);
HeapUp(X/2);
}
}
}
void HeapDown (int X, int N)
{
int Left, Right;
if(2*X<=N)
{
Left=H[2*X];
if(2*X+1<=N)
Right=H[2*X+1];
else Right=Left-1;
if(H[X] < max(Left, Right))
{
if(Left>Right)
{
swap(H[X], H[2*X]);
HeapDown(2*X, N);
}
else
{
swap(H[X], H[2*X+1]);
HeapDown(2*X+1, N);
}
}
}
}
int main()
{
freopen("algsort.in", "r", stdin);
freopen("algsort.out", "w", stdout);
scanf("%d", &N);
for(i=1; i<=N; i++)
{
scanf("%d", &H[i]);
HeapUp(i);
}
Length=N;
for(i=1; i<=N; i++)
{
A[Length]=H[1];
swap(H[1], H[Length]);
Length--;
HeapDown(1, Length);
}
for(i=1; i<=N; i++)
printf("%d ", A[i]);
printf("\n");
return 0;
}