Pagini recente » Cod sursa (job #3201874) | Cod sursa (job #1826764) | Cod sursa (job #75816) | Cod sursa (job #213881) | Cod sursa (job #1812302)
#include <iostream>
#include <fstream>
#define inf (1LL<<32)
#define ll long long
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n,num,h[500005];
void Percolate(int pos)
{
while(pos/2 && h[pos]<h[pos/2])
{ swap(h[pos],h[pos/2]);
pos/=2;
}
}
void Sift(int pos)
{ int ind=0,ok=1;
ll val;
while(ok)
{
ok=0; val=inf;
if (pos*2<=n)
{val=h[pos*2]; ind=pos*2;}
if (pos*2+1<=n && h[pos*2+1]<h[pos*2])
{val=h[pos*2+1]; ind=pos*2+1;}
if (val<h[pos])
{
swap(h[pos],h[ind]);
pos=ind;
ok=1;
}
}
}
void Del(int x)
{
swap(h[x],h[n]);
n--;
if (n && x!=n+1)
{
if (x/2 && h[x]<h[x/2]) Percolate(x);
else Sift(x);
}
}
int main()
{ int x,t,caz,i;
f>>t;
for(i=1;i<=t;i++)
f>>h[i];
n=t;
for(i=n/2;i>=1;i--)
Sift(i);
for(i=1;i<=t;i++)
{ g<<h[1]<<" ";
Del(1);
}
return 0;
}