Cod sursa(job #1974065)

Utilizator al_k_ponyClaudiu Babin al_k_pony Data 26 aprilie 2017 18:43:33
Problema Order Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
# pragma GCC optimize("O3")
# include <bits/stdc++.h>
# define maxn 30001
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define pb push_back
# define mp make_pair
//# define int ll
using namespace std;

int t[4 * maxn],n;

void build(int nod,int l,int r)
{
    if(l == r)
    {
        t[nod] = 1;
        return ;
    }
    int mid = l + r >> 1;
    build(nod << 1,l,mid);
    build(nod << 1 | 1,mid + 1,r);
    t[nod] = t[nod << 1] + t[nod << 1 | 1];
}

void upd(int nod,int l,int r,int pos,int val)
{
    if(l == r)
    {
        t[nod] = val;
        return ;
    }
    int mid = l + r >> 1;
    if(pos <= mid) upd(nod << 1,l,mid,pos,val);
    else upd(nod << 1 | 1,mid + 1,r,pos,val);
    t[nod] = t[nod << 1] + t[nod << 1 | 1];
}

int kth(int nod,int l,int r,int k)
{
    if(k > t[nod]) return n + 1;
    if(l == r) return l;
    int mid = l + r >> 1;
    if(k <= t[nod << 1]) return kth(nod << 1,l,mid,k);
    return kth(nod << 1 | 1,mid + 1,r,k - t[nod << 1]);
}

int32_t main(){_
    freopen("order.in","r",stdin);
    freopen("order.out","w",stdout);
    cin >> n;
    int poss = 2;
    build(1,1,n);
    for(int i = 1;i <= n;i++)
    {
        poss += i - 1;
        poss %= (n - i + 1);
        if(poss == 0) poss = n - i + 1;
        int x = kth(1,1,n,poss);
        cout << x << ' ';
        upd(1,1,n,x,0);
    }
}