Cod sursa(job #2306796)

Utilizator usureluflorianUsurelu Florian-Robert usureluflorian Data 22 decembrie 2018 21:29:55
Problema Order Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <bits/stdc++.h>
#define usu(x) x&(-x)
using namespace std;
ifstream f ("order.in");
ofstream g ("order.out");
const int nmax=6e4+3;
int n,v[nmax],x,t1,t2,t,bun,poz,usut,urm[nmax],ant[nmax],st,dr,mij;
int query(int poz)
{
    int sol=0;
    while(poz)
    {
        sol+=v[poz];
        poz-=usu(poz);
    }
    return sol;
}
void update(int poz,int val)
{
    while(poz<=2*n)
    {
        v[poz]+=val;
        poz+=usu(poz);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    f>>n;
    for(int i=1;i<=2*n;++i) update(i,1);
    poz=2;
    usut=n;
    for(int i=1;i<n;++i) urm[i]=i+1;
    urm[n]=1;
    for(int i=2;i<=n;++i) ant[i]=i-1;
    ant[1]=n;
    for(int i=1;i<=n;++i)
    {
        t=i;
        while(t>=usut+1) t-=usut;
        st=poz;
        dr=poz+n-1;
        t1=query(poz-1);
        while(st<=dr)
        {
            mij=(st+dr)/2;
            t2=query(mij);
            if(t2-t1>=t)
            {
                bun=mij;
                dr=mij-1;
            }
            else st=mij+1;
        }
        if(bun>n) bun-=n;
        update(bun,-1);
        update(bun+n,-1);
        //cout<<poz<<' '<<t<<' '<<bun<<'\n';
        g<<bun<<' ';
        t1=ant[bun];
        t2=urm[bun];
        urm[t1]=t2;
        ant[t2]=t1;
        poz=t2;
        --usut;
    }
    return 0;
}