Cod sursa(job #2090969)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 18 decembrie 2017 21:37:50
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <bits/stdc++.h>

#define pb push_back 
#define x first 
#define y second 
#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair< int , int > PII;
const int dirx[] = {-2,-1,1,2, 1, -1};
const int diry[] = {0,1,1,0, -1, -1};
inline void debugMode() {
    #ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
    #endif
}

int pozr;
char buffer[1010];
FILE *fi, *fo;
         
char getch(){
    if( pozr == 1010 ){
        fread( buffer, 1010, 1, fi  );
        pozr = 0;
    }
    return buffer[ pozr++ ];
}
         
int scano(){
    int nr = 0;
    char ch = getch();
    while( isdigit(ch) == 0 )
        ch = getch();
    while( isdigit(ch) != 0 ){
        nr = nr * 10 + ch - '0';
        ch = getch();
    }
    return nr;
}

int n, m, a[500100], AINT[2000100];

void update(int Node, int Left, int Right, int Pos){
	if (!(Right - Left)){
		AINT[Node] = Pos;
		return ; 
	}

	int mid = (Left + Right) >> 1;
	if (Pos <= mid) update(Node << 1, Left, mid, Pos);
	else update(Node << 1 | 1, mid + 1, Right, Pos);

	int idx1 = AINT[Node << 1], idx2 = AINT[Node << 1 | 1];
	AINT[Node] = (a[idx1] < a[idx2] ? idx1 : idx2);
}

int main(){
	//debugMode();
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	fi = fopen("algsort.in", "r");
	fo = fopen("algsort.out", "w");
  
	n = scano();
	for (int i = 1; i <= n; i++){
		a[i] = scano();
		update(1, 1, n, i);
	}

	for (int i = 1; i <= n; i++){
		fprintf(fo, "%d ", a[ AINT[1] ]);
		a[ AINT[1] ] = INT_MAX;
		update(1, 1, n, AINT[1]);
	}
	
	return 0;
}