Cod sursa(job #2301008)

Utilizator ValentinSavoiuFMI Savoiu Valentin-Marian ValentinSavoiu Data 12 decembrie 2018 15:09:00
Problema Sortare prin comparare Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 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], B[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() {
    int x, a, b;
    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];
        B[i] = A[pos];
        A[pos] = MAX_INT;
        Update(1, 1, N, pos);
    }

    for (int i = 1; i <= N; ++i) {
        g << B[i] << ' ';
    }
}