Cod sursa(job #2020107)

Utilizator rares96cheseliRares Cheseli rares96cheseli Data 9 septembrie 2017 14:01:48
Problema Schi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>

using namespace std;

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

int N, step = 1, aib[30005], a[30005], sol[300005];

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

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

int query(int pos) {
    int res = 0;
    while (pos) {
        res += aib[pos];
        pos -= lsb(pos);
    }

    return res;
}

int main() {
    f >> N;
    
    for (int i = 1; i <= N; ++i) {
        f >> a[i];
        update(i, 1);
    }

	for (; step <= N; step <<= 1);    
    for (int i = N; i > 0; --i) {
        int ps = 0;

        for (int j = step; j; j >>= 1) {
        	if (ps + j <= N && aib[ps + j] < a[i]) {
        		ps += j;
        		a[i] -= aib[ps];
        	}
        }

        sol[ps + 1] = i;
        update(ps + 1, -1);
    }
    
    for (int i = 1; i <= N; ++i) {
        cout << sol[i] << '\n';
    }
    return 0; 
}