Pagini recente » Cod sursa (job #3362540) | Cod sursa (job #3362581)
#include <bits/stdc++.h>
using namespace std;
const int N=30001;
int fen[N];
void update(int i, int add)
{
while(i<N)
{
fen[i]+=add;
i+=(i&(-i));
}
}
int sum(int i)
{
int s=0;
while(i)
{
s+=fen[i];
i-=(i&(-i));
}
return s;
}
int find_kth(int n)
{
int st=1,dr=N-1;
while(st<=dr)
{
int mid=(st+dr)/2;
if(sum(mid)>=n)
{
dr=mid-1;
}
else st=mid+1;
}
return st;
}
int main ()
{
freopen("order.in","r",stdin);
freopen("order.out","w",stdout);
int n;
cin>>n;
for (int i=1; i<=n; i++)
{
update(i,1);
}
int x=1;
for(int i=1; i<=n; i++)
{
int copil=n-i+1;
x=(x+i-1)%copil+1;
int cop=find_kth(x);
cout<<cop<<" ";
update(cop,-1);
if(x>copil-1)
x=1;
}
}