Cod sursa(job #3132690)

Utilizator MihaicrissStrejaru Mihai-Cristian Mihaicriss Data 23 mai 2023 16:21:05
Problema Schi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <iostream>
#include <fstream>
using namespace std;

const int MAXN = 200005;

int n, m, i, j, aib[MAXN], v[MAXN], ans[MAXN];

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

int query(int pos) {
    int result = 0;
    while (pos > 0) {
        result += aib[pos];
        pos -= pos & (-pos);
    }
    return result;
}

int binarySearch(int elem) {
    int left = 1, right = n;
    while (left <= right) {
        int mid = (left + right) / 2;
        if (query(mid) < elem)
            left = mid + 1;
        else
            right = mid - 1;
    }
    return left;
}

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

    fin >> n;
    for (i = 1; i <= n; i++) {
        fin >> v[i];
        update(i, 1);
    }

    for (i = n; i >= 1; i--) {
        int l = binarySearch(v[i]);
        ans[l] = i;
        update(l, -1);
    }

    for (i = 1; i <= n; i++)
        fout << ans[i] << '\n';

    fin.close();
    fout.close();

    return 0;
}