Cod sursa(job #2301012)

Utilizator ValentinSavoiuFMI Savoiu Valentin-Marian ValentinSavoiu Data 12 decembrie 2018 15:16:48
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#define dim 500001
#define MAX_INT 2147483647
using namespace std;

ifstream f ("algsort.in");
ofstream g ("algsort.out");

int tree[4 * dim + 13], A[dim];
int pos, N, M;
void Update(int nod, int currL, int currR, int pos) {
     if ( currL == currR ) {
          tree[nod] = pos;
          return;
     }

     int mid = currL + (currR - currL) / 2;
     if ( pos <= mid ) Update(2 * nod, currL, mid, pos);
     else              Update(2 * nod + 1, mid + 1, currR, pos);

     tree[nod] = (A[tree[2 * nod]] < A[tree[2 * nod + 1]]) ? tree[2 * nod] : tree[2 * nod + 1];
}

int main() {
    f >> N;

    for (int i = 1; i <= N; i++) {
        f >> A[i];
        Update(1, 1, N, i);
    }
    for (int i = 1; i < N; ++i) {
        pos = tree[1];
        g << A[pos] << ' ';
        A[pos] = MAX_INT;
        Update(1, 1, N, pos);
    }
    g << A[tree[1]];
}