Cod sursa(job #3377287)

Utilizator Andreea1501013Andreea Andreea1501013 Data 22 noiembrie 2026 19:29:58
Problema Order Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.83 kb
#include <bits/stdc++.h>

using namespace std;

int N;
bool ok = 1;
int aib[60005];

void update(int i, int val)
{
    int n = 2 * N;
    for(int x = i; x <= n; x += (x&(-x)))
    {
        aib[x] += val;
    }
}

void generareAIB()
{
    for(int i = 1; i <= 2*N; i++)
    {
        update(i,1);
    }
}

int query(int i)
{
    int sum = 0;
    for(int x = i; x >= 1; x -= (x&(-x)))
    {
        sum += aib[x];
    }
    return sum;
}

/*bool firstRotation(int i, int cnt)
{
    int ci = i + cnt;
    if(ci > N)
    {
        ok = 0;
    }
    return ok;
}*/

bool check(int i, int j, int ad)
{
    int p = query(j) - query(i);
    if(p < ad)
    {
        return 1;
    }
    return 0;
}

int cautareBinara(int i, int ad)
{
    int l = i, r = 2 * N,sol = i-1;
    while(l <= r)
    {
        int m = (l + r)/ 2;
        if(check(i, m, ad))
        {
            sol = m;
            l = m + 1;
        }
        else
        {
            r = m - 1;
        }
    }
    return sol + 1;
}

int main()
{
    ifstream cin("order.in");
    ofstream cout("order.out");

    cin>>N;
    generareAIB();
    int ramase = N, cnt = 1, ad, i = 1;
    while(ramase > 0)
    {
        ad = cnt;
        ad %= ramase;
        if(ad == 0)
        {
            ad = ramase;
        }
      /*  if(firstRotation(i, cnt))
        {
            i += cnt;
            cout<<i<<' ';
            update(i);
            update(i + N);
        }
        else
        {*/
            int j = cautareBinara(i, ad);
            j %= N;
            if(j == 0)
            {
                j = N;
            }
            update(j, -1);
            update(j + N, -1);
            i = j;
            cout<<j<<' ';
        //}
        cnt++;
        ramase--;
    }
    return 0;
}