Cod sursa(job #2300288)

Utilizator andreinichitaTirziu Nichita andreinichita Data 11 decembrie 2018 09:15:08
Problema Order Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <cstdio>

using namespace std;
int n, aib[30005];
void update(int poz,int x)
{
    for(;poz<=n;poz+=poz&(-poz))
        aib[poz]+=x;
}
int query(int poz)
{
 int s=0;
 for(;poz>=1;poz-=poz&(-poz))
        s+=aib[poz];
 return s;
}
int bs(int val)
{
    int st=1,dr=n,med,rez;
    while(st<=dr)
    {
        med=(st+dr)/2;
        rez=query(med);
        if(val==rez)
            return med;
        if(val<rez)
            dr=med-1;
        else
            st=med+1;
    }
    return st-1;
}
int main()
{
    freopen("order.in","r",stdin);
    freopen("order.out","w",stdout);
    int i,x=2,poz;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        update(i,1);
    for(i=1;i<=n;i++)
    {
        x=(x+i-2)%(n+1-i)+1;
        poz=bs(x);
        update(poz,-1);
        printf("%d ",poz);
    }
    return 0;
}