Cod sursa(job #1355531)

Utilizator delia_99Delia Draghici delia_99 Data 22 februarie 2015 20:12:47
Problema Order Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <cstdio>
#define ub(x) (x&(-x))
using namespace std;
int n,i,AIB[30010],pos,ind,P;

int sum(int x)
{
    int i,s=0;
    for(i=x; i>0; i-=ub(i))
        s+=AIB[i];

    return s;
}

int binary_search(int P)
{
    int st=1,dr=n,m,s,Min=30001;
    while(st<=dr)
    {
        m=st+(dr-st)/2;
        s=sum(m);
        if(s==P && Min>m)
            Min=m;
        else
        {
            if(s>=P)
                dr=m-1;
            else st=m+1;
        }
    }
    return Min;
}

void remove(int x)
{
    int i;
    for(i=x; i<=n; i+=ub(i))
        --AIB[i];
    return;

}

int main()
{
    freopen("order.in","r",stdin);
    freopen("order.out","w",stdout);
    scanf("%d",&n);
    for(i=1; i<=n; ++i)
        AIB[i]=ub(i);
    ind=n;
    P=2;
    for(i=1; i<=n; ++i)
    {
        --ind;
        pos=binary_search(P);
        remove(pos);
        P+=i;
        if(ind!=0)
        {
            P=P%ind;
            if(P==0)
                P=ind;
        }
        printf("%d ",pos);
    }
    printf("\n");
    return 0;
}