Cod sursa(job #2489476)

Utilizator AnduebossAlexandru Ariton Andueboss Data 8 noiembrie 2019 20:10:36
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
//
//  main.cpp
//  c++joc
//
//  Created by Andu Andu on 07/11/2019.
//  Copyright © 2019 Andu Andu. All rights reserved.
//

#include <fstream>
#define N 500001

using namespace std;

ifstream cin ("algsort.in");
ofstream cout ("algsort.out");

int n,m;
int h[N];
void maxx() {
    cout<<h[1]<<" ";
}

void add(int newElem) {
    n++;
    int poz = n;
    h[n] = newElem;
    while (poz>1 && h[poz/2] > h[poz]) {
        swap(h[poz/2], h[poz]);
        poz /= 2;
    }
}

void popFirst() {
    int poz = 1;
    h[1] = h[n];
    h[n] = 0;
    n--;
    while (2*poz <= n) {
        poz *= 2;
        if (poz<n && h[poz+1] < h[poz]) {
            poz++; //trec la celalalt copil
        }
        if (h[poz] < h[poz/2]) {
            swap(h[poz], h[poz/2]);
        } else poz = n; // ca sa iasa
    }
}

int main() {
    cin>>m;
    int v;
    for (int i=1; i<=m; i++) {
        cin>>v;
        add(v);
    }
    for (int i=1; i<=m; i++) {
        maxx();
        popFirst();
    }
    return 0;
}