Pagini recente » Cod sursa (job #1735901) | Cod sursa (job #741463) | Cod sursa (job #2553277) | Cod sursa (job #1478785) | Cod sursa (job #2107161)
#include <stdio.h>
#include <algorithm>
#include <climits>
using namespace std;
FILE* fin;
FILE* fout;
int arb[2000001];
int n;
int V[500000];
int queryTree()
{
int node = 1;
int left = 1;
int right = n + 1;
while(arb[node * 2] || arb[node * 2 + 1])
{
if(arb[node * 2] == arb[1])
node *= 2;
else if(arb[node * 2 + 1] == arb[1])
(node *= 2)++;
}
if(arb[node] == arb[1])
return arb[node];
return arb[node + 1];
}
void update(int node, int x, int pos, int left, int right)
{
if(left == pos && right == pos)
arb[node] = x;
else if(left > pos || right < pos)
return;
else{
int mij = (left + right) / 2;
update(node * 2, x, pos, left, mij);
update(node * 2 + 1, x, pos, mij + 1, right);
arb[node] = min(arb[node * 2], arb[node * 2 + 1]);
}
}
int main()
{
fin = fopen("algsort.in", "r");
fout = fopen("algsort.out", "w");
fscanf(fin, "%d", &n);
for(int i = 0; i < n; ++i)
{
fscanf(fin, "%d", V + i);
update(1, V[i], i + 1, 1, n);
}
for(int i = 0; i < n; ++i)
{
fprintf(fout, "%d ", arb[1]);
int aux = queryTree();
for(int j = 0; j < n; ++j)
if(V[j] == aux)
{
update(1, INT_MAX, j + 1, 1, n);
V[j] = -1;
break;
}
}
return 0;
}