Cod sursa(job #3267116)

Utilizator Ruxandra009Ruxandra Vasilescu Ruxandra009 Data 11 ianuarie 2025 09:47:22
Problema Schi Scor 0
Compilator cpp-64 Status done
Runda cex_6 Marime 1.04 kb
#include <algorithm>
#include <fstream>
#include <vector>
#include <map>

using namespace std;

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

const int nmax = 30005;
int n, m, aib[nmax], v[nmax], ans[nmax];

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

void add(int poz, int val)
{
    for(int i = poz; i <= n; i += ub(i))
        aib[i] += val;
}

int sum(int poz)
{
    int ans = 0;
    for(int i = poz; i >= 1; i -= ub(i))
        ans += aib[i];

    return ans;
}

int main()
{
    f >> n;
    for(int i = 1; i <= n; i ++)
        f >> v[i];

    for(int i = 1; i <= n; i ++)
        add(i, 1);

    for(int i = n; i >= 1; i --)
    {
        int st = 1, dr = n, poz = 0;
        while(st <= dr)
        {
            int mij = (st + dr) / 2;

            if(sum(mij) >= v[i])
                poz = mij, dr = mij - 1;
            else
                st = mij + 1;
        }

        add(poz, -1);

        ans[poz] = i;
    }

    for(int i = 1; i <= n; i ++)
        g << ans[i] << '\n';
    return 0;
}