Cod sursa(job #1250720)

Utilizator lorundlorund lorund Data 28 octombrie 2014 14:07:23
Problema Schi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <cstdio>
#define SIZE 30005

int n, v[SIZE];
int aib[SIZE], sol[SIZE];

void update(int x, int val){
    for (; x<=n; x+=x&(x-1)^x)
        aib[x] += val;
}

int query(int x){
    int sum = 0;

    for (; x; x-=x&(x-1)^x)
        sum += aib[x];
    return sum;
}

int bsearch(int x){
    int li=0, ls=n;
    int sol = ls;

    while (li<=ls){
        int m=(li+ls)/2;

        if (query(m)>=x){
            sol = m;
            ls = m-1;
        }
        else{
            li = m+1;
        }
    }
    return sol;
}

int main()
{
    freopen("schi.in", "r", stdin);
    freopen("schi.out", "w", stdout);

    scanf("%d", &n);
    for (int i=1; i<=n; ++i){
        scanf("%d", &v[i]);
        update(i, 1);
    }
    for (int i=n; i; --i){
        int pos = bsearch(v[i]);
        sol[pos] = i;
        update(pos, -1);
    }
    for (int i=1; i<=n; ++i){
        printf("%d\n", sol[i]);
    }
    return 0;
}