Cod sursa(job #2090962)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 18 decembrie 2017 21:31:40
Problema Sortare prin comparare Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 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("algsort.in", "r", stdin);
    freopen("algsort.out", "w", stdout);
    #endif
}

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);
	cin >> n;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		update(1, 1, n, i);
	}

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