Cod sursa(job #3206098)

Utilizator gBneFlavius Andronic gBne Data 21 februarie 2024 17:32:42
Problema Schi Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax = 30005;

stack<pair<int,int>> s;
int sol[NMax], AIB[NMax], n;

void update(int val, int poz){
    for(int i=poz; i<=n; i+=(i & -i)){
        AIB[i] += val;
    }
}

int query(int poz){
    int ans = 0;
    for(int i=poz; i>0; i-=(i & -i)){
        ans += AIB[i];
    }
    return ans;
}

int cauta(int val){
    int st = 1, dr = n;
    int mij = (st + dr) / 2;
    while(st <= dr){
        mij = (st + dr) / 2;
        int cautaVal = query(mij);
        if(cautaVal == val){
            while(query(mij - 1) == cautaVal){
                mij --;
            }
            return mij;
        }
        else if(cautaVal > val){
            dr = mij - 1;
        }
        else{
            st = mij + 1;
        }
    }
    return -1;
}

int main()
{
    fin >> n;
    for(int x, i=1; i<=n; i++){
        fin >> x;
        s.push(make_pair(x, i));
        update(1, i);
    }
    while(!s.empty()){
        int concurent = s.top().first;
        int index = s.top().second;
        int poz_update = cauta(concurent);
        sol[poz_update] = index;
        update(-1, poz_update);
        s.pop();
    }
    for(int i=1; i<=n; i++){
        fout << sol[i] << '\n';
    }
    return 0;
}