Pagini recente » Borderou de evaluare (job #870345) | Cod sursa (job #1164175) | Cod sursa (job #2775853) | Cod sursa (job #3288239) | Cod sursa (job #1784814)
#include <fstream>
//#include <iostream>
#include <math.h>
using namespace std;
ifstream ka("algsort.in");
ofstream ki("algsort.out");
const int N_MAX = 500000;
const int SQRT_MAX = 708;
int n, x, sol[N_MAX + 1], v[N_MAX + 1], bb[SQRT_MAX + 1];
int main()
{
ka >> n;
int t = (int)sqrt(n) + 1;
for(int i = 0; i <= t; i++)
bb[i] = 0x7fffffff;
for(int i = 0; i < n; i++)
{
ka >> x;
v[i] = x;
bb[i / t] = min(bb[i / t], x);
}
int bagate = 0;
while(bagate < n)
{
int minim = 0x7fffffff, poz;
for(int i = 0; i <= t; i++)
if(bb[i] < minim)
{
minim = bb[i];
poz = i;
}
sol[++bagate] = minim;
int c1 = poz * t, c2 = (poz + 1) * t;
bool gasit = false;
for(int i = c1; i < n && i < c2 && !gasit; i++)
if(v[i] == minim)
{
v[i] = 0x7fffffff;
//cout << i << " ";
gasit = true;
}
minim = 0x7fffffff;
bb[poz] = 0x7fffffff;
for(int i = c1; i < n && i < c2; i++)
bb[i / t] = min(bb[i / t], v[i]);
}
for(int i = 1; i <= n; i++)
ki << sol[i] << " ";
}