Pagini recente » Cod sursa (job #1278216) | Cod sursa (job #1582960) | Cod sursa (job #129424) | Cod sursa (job #1495246) | Cod sursa (job #3123977)
#include <bits/stdc++.h>
using namespace std;
ifstream in ("algsort.in");
ofstream out ("algsort.out");
const int NMAX = 5e5;
struct heap
{
private:
int n;
int h[NMAX + 5];
public:
heap() {}
void up (int p)
{
while (p > 1 & h[p] < h[p / 2])
{
swap(h[p], h[p / 2]);
p /= 2;
}
}
void down (int p)
{
while (2*p <= n)
{
int q = p;
if (h[2*p] < h[q])
q = 2*p;
if (2*p < n && h[2*p+1] < h[q])
q = 2*p+1;
if (p == q) break;
swap(h[p], h[q]);
p = q;
}
}
void push (int x)
{
h[++n] = x;
up(n);
}
void pop()
{
h[1] = h[n--];
up(1);
down(1);
}
int top()
{
return h[1];
}
};
int main()
{
heap h;
int n;
in >> n;
for (int i=1; i<=n; i++)
{
int x;
in >> x;
h.push(x);
}
for (int i=1; i<=n; i++)
{
out << h.top() << ' ';
h.pop();
}
return 0;
}