Pagini recente » Cod sursa (job #1190135) | Cod sursa (job #1775725) | Cod sursa (job #1942869) | Cod sursa (job #2037264) | Cod sursa (job #1051189)
#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
//#include <unordered_map>
#define NMAX 500001
int N, M;
int ArbInt[2 * NMAX + 4000];
int X, start, finish, val, pos, minim, pozArray, ArrayWithPos[2 * NMAX + 4000], posInitial;
inline int Minim(int a, int b)
{
if ( a < b )
return a;
return b;
}
void Actualizare(int nod, int st, int dr)
{
if ( st == dr )
{
ArbInt[nod] = val;
ArrayWithPos[nod] = posInitial;
return;
}
int mid = (st + dr) / 2;
if ( pos <= mid )
Actualizare (2 * nod, st, mid);
else
Actualizare (2 * nod + 1, mid + 1, dr);
ArbInt[nod] = Minim ( ArbInt[2 * nod], ArbInt[2 * nod + 1] );
ArrayWithPos[nod] = ( ArbInt[2 * nod] < ArbInt[2 * nod + 1] ? ArrayWithPos[2 * nod] : ArrayWithPos[2 * nod + 1] );
}
void Interogare (int nod, int st, int dr)
{
if ( start <= st && dr <= finish )
{
if ( minim > ArbInt[nod] )
{
minim = ArbInt[nod];
pozArray = ArrayWithPos[nod];
}
return;
}
int mid = (st + dr) / 2;
if ( start <= mid )
Interogare (2 * nod, st, mid);
if ( mid + 1 <= finish )
Interogare (2 * nod + 1, mid + 1, dr);
}
int main()
{
FILE *f = fopen("algsort.in", "r");
FILE *g = fopen("algsort.out" , "w");
fscanf (f, "%d", &N);
for ( posInitial = 1; posInitial <= N; posInitial++ )
{
fscanf (f, "%d", &X);
pos = posInitial;
val = X;
Actualizare (1, 1, N);
}
minim = ( 1<<30 );
for ( int i = 1; i <= N; i++ )
{
start = 1; finish = N;
Interogare(1, 1, N);
fprintf (g, "%d ", minim);
pos = pozArray;
val = ( 1<<30 );
Actualizare (1, 1, N);
minim = ( 1<<30 );
}
fclose(f); fclose(g);
return 0;
}