Cod sursa(job #2489524)

Utilizator DragosSDragos Sarbu DragosS Data 8 noiembrie 2019 21:06:23
Problema Order Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include<bits/stdc++.h>

using namespace std;

ifstream f("order.in");
ofstream g("order.out");

int n, aib[30005];

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

int suma(int p)
{
    int s=0;
    while(p>0)
    {
        s+=aib[p];
        p-=(p&(-p));
    }
    return s;
}

int firstpos(int x)
{
    int pos=0,pas=(1<<15);

    while(pas > 0)
    {
        if(pos+pas<=n && aib[pos+pas]<x)
        {
            pos+=pas;
            x-=aib[pos];
        }
        pas/= 2;
    }
    return pos+1;
}

int main()
{
    int x=1,i,j,posn,posx;
    f>>n;
    for(int i=1;i<= n;i++)
        update(i,1);

    for(j=1;j<=n;j++)
    {
        i=j%(n-j+1);
        if (i==0)
            i=n-j+1;

        posn = suma(n);
        posx = suma(x);

        if(posn - posx >= i)
            x = firstpos(posx + i);
        else
            x = firstpos(posx-posn+i);

        g<<x<<" ";
        update(x,-1);
    }
}