Cod sursa(job #3372663)

Utilizator Cristian_NegoitaCristian Negoita Cristian_Negoita Data 4 noiembrie 2026 17:28:51
Problema Order Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("order.in");
ofstream fout("order.out");
#define lsb(i) (i & (-i))
const int NMAX = 30005, LOG = 14;
int n;

class FenwickTree
{
private:
    int tree[NMAX];

public:
    void update(int x, int val)
    {
        for(int i = x; i <= n; i += lsb(i))
            tree[i] += val;
    }

    int caut_bin(int x)
    {
        int ans = 0, sum = 0;
        for(int step = (1 << LOG); step > 0; step >>= 1)
        {
            if(ans + step <= n && sum + tree[ans + step] < x)
            {
                sum += tree[ans + step];
                ans += step;
            }
        }
        return (ans + 1);
    }
};
FenwickTree aib;

int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++)
        aib.update(i, 1);
    int rem = n, last = 1;
    for(int i = 1; i <= n; i++)
    {
        int elim = (last + i - 1) % rem;
        int copil = aib.caut_bin(elim + 1);
        fout << copil << " ";
        aib.update(copil, -1);
        rem--;
        last = elim;
    }

    fin.close();
    fout.close();
    return 0;
}