Cod sursa(job #2094032)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 24 decembrie 2017 21:13:36
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 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, Heap[500100];

void down(int p){
	int son;
	do{
		son = (p << 1) * (p << 1 <= n) + ((p << 1 | 1) <= n && Heap[p << 1 | 1] < Heap[p << 1]);
		son *= (Heap[son] < Heap[p]); 
		if (son){
			swap(Heap[son], Heap[p]);
			p = son;
		}
	} while (son);
}

void up(int p){
	while (p > 1 && Heap[p] < Heap[p >> 1]){
		swap(Heap[p], Heap[p >> 1]);
		p >>= 1;
	}
}

void insert(int val){
	Heap[n] = val;
	up(n);
}

void cut_head(){
	cout << Heap[1] << " ";
	Heap[1] = Heap[n--];
	down(1);
}

int main(){
	debugMode();
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> m;
	for (int i = 1, x; i <= m; i++){
		++n;
		cin >> x;
		insert(x);
	}
	
	for (int i = 1; i <= m; i++) cut_head();

	return 0;
}