Cod sursa(job #2673537)

Utilizator tomaionutIDorando tomaionut Data 17 noiembrie 2020 10:00:11
Problema Schi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("schi.in");
ofstream fout("schi.out");
int a[30005],aib[30005],sol[30005],n;


void update(int p, int x)
{
    while (p<=n)
    {
        aib[p]+=x;
        p+=(p&(-p));
    }

}

int Suma(int p)
{
    int s=0;
    while (p>0)
    {
        s+=aib[p];
        p-=(p&(-p));
    }
    return s;
}

int CautBin(int x)
{
    int st=1,dr=n,mij,poz=-1,s;
    while (st<=dr)
    {
        mij=(st+dr)/2;
        s=Suma(mij);
        if (s==x)
        {
            poz=mij;
            dr=mij-1;
        }
        else if (s<x) st=mij+1;
        else dr=mij-1;
    }
    return poz;
}
int main()
{
    int poz,x,i;
    fin >> n;
    for (i=1; i<=n; i++)
    {
        fin >> a[i];
        update(i,1);
    }
    for (i=n; i>=1; i--)
    {
        poz=CautBin(a[i]);
        update(poz,-1);
        sol[poz]=i;
    }
    for (i=1; i<=n; i++)
        fout << sol[i] << "\n";

    return 0;
}