Cod sursa(job #2432256)

Utilizator bluestorm57Vasile T bluestorm57 Data 22 iunie 2019 18:23:22
Problema Order Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("order.in");
ofstream g("order.out");

const int NMAX = 300005;
int arb[8 * NMAX],n,pos = 1;

void build(int nod, int lo, int hi){
    if(lo == hi){
        arb[nod] = 1;
        return;
    }
    int mid = (lo + hi) / 2;
    build(2 * nod, lo, mid);
    build(2 * nod + 1, mid + 1, hi);
    arb[nod] = arb[2 * nod] + arb[2 * nod + 1];
}

void update(int nod, int lo, int hi, int x){
    if(lo == hi){
        g << lo << " ";
        arb[nod] = 0;
        return;
    }
    int mid = (lo + hi) / 2;
    if(x <= arb[2 * nod])
        update(2 * nod, lo, mid, x);
    else
        update(2 * nod + 1, mid + 1, hi, x - arb[2 * nod]);
    arb[nod] = arb[2 * nod + 1] + arb[2 * nod];

}

int main(){
    int i;
    f >> n;
    build(1,1,n);

    for(i = 1 ; i <= n ; i++){
        //if(arb[i] > 1){
            if(pos + i <= arb[1]){
                pos += i;
                update(1,1,n, pos);
                //g << pos  << "\n";
                pos--;
            }else{
                //pos = pos - i - arb[1] - 1;
                //pos %= arb[1];
                pos = (pos + i - 1) % arb[1] + 1;
                update(1,1,n,pos);
                //g << pos << "\n";
                pos--;
            }
        //}else
            //update(1,1,n,1);

    }

    return 0;
}