Pagini recente » Cod sursa (job #392786) | Cod sursa (job #1552351) | Cod sursa (job #1514390) | Cod sursa (job #1670189) | Cod sursa (job #2489524)
#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);
}
}