Cod sursa(job #3134565)

Utilizator Maftei_TudorMaftei Tudor Maftei_Tudor Data 29 mai 2023 16:55:05
Problema Schi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>

#define DIM 30000

using namespace std;
ifstream fin("schi.in");
ofstream fout("schi.out");

int n, v[DIM+1], aib[DIM+1], ans[DIM+1];

int lsb(int x) {
    return x & (-x);
}

int query(int pos) {
    int ans = 0;

    while(pos) {
        ans += aib[pos];
        pos -= lsb(pos);
    }

    return ans;
}

void update(int pos, int val) {
    while(pos <= n) {
        aib[pos] += val;
        pos += lsb(pos);
    }
}

int cb(int x) {
    int pw = 1, pos = 0;
    while(pw <= n)
        pw *= 2;
    pw /= 2;

    while(pw) {
        if(pos + pw <= n)
            if(query(pos + pw) < x)
                pos += pw;

        pw /= 2;
    }

    return pos + 1;
}

int main()
{
    fin >> n;
    for(int i=1; i<=n; i++) {
        fin >> v[i];
        update(i, 1);
    }


    for(int i=n; i>=1; i--) {
        int rez = cb(v[i]);
        ans[rez] = i;
        update(rez, -1);
    }

    for(int i=1; i<=n; i++)
        fout << ans[i] << '\n';

    return 0;
}