Cod sursa(job #2902094)

Utilizator RaduAntoneoAntonio Alexandru Radu RaduAntoneo Data 15 mai 2022 16:16:43
Problema Order Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include<bits/stdc++.h> 
using namespace std;

ifstream f("order.in");
ofstream g("order.out");
#define cin f
#define cout g

int aInt[120000], copil = 2, pas = 1, n;

void build(int nod, int st, int dr) {
    if(st == dr) {
        aInt[nod] = 1;
        return;
    }
    int mij = (st + dr) / 2;
    build(nod * 2, st, mij);
    build(nod * 2 + 1, mij + 1, dr);
    aInt[nod] = aInt[nod * 2] + aInt[nod * 2 + 1];
}

void update(int nod, int st, int dr, int copil) {
    if(st == dr) {
        aInt[nod] = 0;
        cout << st << " ";
        return;
    }
    int mij = (st + dr) / 2;
    if(copil <= aInt[nod * 2]) 
        update(nod * 2, st, mij, copil);
    else {
        copil -= aInt[nod * 2];
        update(nod * 2 + 1, mij + 1, dr, copil);
    }
    aInt[nod]--;
}

int main() {
    cin >> n;
    build(1, 1, n);
    while(aInt[1] > 0) {
        copil += pas - 1;
        while(copil > aInt[1])
            copil -= aInt[1];
        update(1, 1, n, copil);
        pas++;
    }
}