Cod sursa(job #3377286)

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

using namespace std;

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

void generareAIB()
{
    int n = 2 * N;
    for(int x = 1; x <= n; x++)
    {
        aib[x]++;
        int tata = x + (x & (-x));
        if(tata <= n)
        {
            aib[tata] += aib[x];
        }
    }
}

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

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 + 1);
            if(j == 0)
            {
                j++;
            }
            update(j);
            update(j + N);
            i = j;
            cout<<j<<' ';
        //}
        cnt++;
        ramase--;
    }
    return 0;
}