Cod sursa(job #2637710)

Utilizator popoviciAna16Popovici Ana popoviciAna16 Data 24 iulie 2020 13:46:17
Problema Order Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.03 kb
#include <fstream>
using namespace std;

ifstream fin("order.in");
ofstream fout("order.out");

//t = numarul total al copiilor, s = copilul de la care plec
//1. sts = querysum pe 1...s
//2. daca pasul p <= t-sts, atunci caut al (sts+p)-lea
//2.5. altfel, merg la (p-(t-sts))%t, daca am 0 pozitia atunci ma duc la t
//3. s = copilul la care ajung, fac update

int a[120001];

int qdr, poz;

void constr(int nod, int st, int dr)
{
    if (st == dr)
        a[nod] = 1;
    else
    {
        int mij = (st+dr)>>1;
        constr(nod<<1, st, mij);
        constr(nod<<1|1, mij+1, dr);
        a[nod] = a[nod<<1] + a[nod<<1|1];
    }
}

void update(int nod, int st, int dr)
{
    if (st == dr)
        a[nod] = 0;
    else
    {
        int mij = (st+dr)>>1;
        if (poz <= mij)
            update(nod<<1, st, mij);
        else
            update(nod<<1|1, mij+1, dr);
        a[nod] = a[nod<<1] + a[nod<<1|1];
    }
}

int querysum(int nod, int st, int dr)
{
    if (dr <= qdr)
        return a[nod];
    int mij = (st+dr)>>1;
    int zwei = 0;
    if (mij < qdr)
        zwei = querysum(nod<<1|1, mij+1, dr);
    return querysum(nod<<1, st, mij) + zwei;
}

int queryk(int n, int k)
{
    int nod = 1, st = 1, dr = n, mij;
    while (st != dr)
    {
        mij = (st+dr)>>1;
        if (a[nod<<1] >= k)
        {
            dr = mij;
            nod = nod << 1;
        }
        else
        {
            k = k - a[nod<<1];
            st = mij + 1;
            nod = nod<<1|1;
        }
    }
    return st;
}

int main()
{
    int i, n, sts, t, s;
    int p;
    fin >> n;
    constr(1, 1, n);
    t = n;
    s = 1;
    for (i = 1; i<=n; i++, t--)
    {
        qdr = s;
        sts = querysum(1, 1, n);
        //fout << sts << '\n';
        if (i <= t - sts)
        {
            s = poz = queryk(n, sts+i);
            update(1, 1, n);
        }
        else
        {
            p = (i+sts)%t;
            if (p == 0)
                p = t;
            s = poz = queryk(n, p);
            update(1, 1, n);
        }
        fout << s << ' ';
    }
    return 0;
}