Pagini recente » Cod sursa (job #2344453) | Cod sursa (job #2653020) | Cod sursa (job #1351109) | Cod sursa (job #20437) | Cod sursa (job #2436583)
#include <bits/stdc++.h>
#define maxn 500000
using namespace std;
ifstream fin ( "algsort.in" );
ofstream fout ( "algsort.out" );
struct nod
{
int lson, rson, val, amt;
};
int v[maxn+5];
nod g[maxn+5];
void add_val ( int p, int x )
{
if (g[p].val == v[x]) { g[p].amt++; return; }
if (g[p].val < v[x])
{
if (g[p].rson == 0) g[p].rson = x, g[x] = {0, 0, v[x], 1};
else add_val (g[p].rson, x);
}
else
{
if (g[p].lson == 0) g[p].lson = x, g[x] = {0, 0, v[x], 1};
else add_val (g[p].lson, x);
}
}
void dfs ( int x )
{
if (g[x].lson != 0) dfs (g[x].lson);
for (int i = 0; i < g[x].amt; i++ ) fout << g[x].val << ' ';
if (g[x].rson != 0) dfs (g[x].rson);
}
int main ()
{
int n; fin >> n;
int i, j, z;
for ( i = 1; i <= n; i++ ) fin >> v[i];
g[1] = {0, 0, v[1], 1};
for ( i = 2; i <= n; i++ ) add_val (1, i);
dfs (1);
return 0;
}