Cod sursa(job #852816)

Utilizator sternvladStern Vlad sternvlad Data 11 ianuarie 2013 19:48:12
Problema Order Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include<fstream>

using namespace std;

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

int suma[65600];
int poz,val;

void update(int nod, int st,int dr)
{
    if(st==dr)suma[nod]=val;
    else
        {
            int m;
            m=(st+dr)/2;
            if(poz<=m)update(nod*2,st,m);
                else update(nod*2+1,m+1,dr);
            suma[nod]=suma[nod*2]+suma[nod*2+1];
        }
}
void cautaBinar(int nod,int st,int dr,int cat)
{
    if(st==dr)poz=st;
    else{
          int m;
          m=(st+dr)/2;
          if(cat<=suma[nod*2])cautaBinar(nod*2,st,m,cat);
            else cautaBinar(nod*2+1,m+1,dr,cat-suma[nod*2]);
      }

}
int main()
{
    int n,i;
    fin>>n;
    for(poz=1;poz<=n;poz++)
    {
        val=1;
        update(1,1,n);
    }
        int cat;cat=1;
        for(i=1;i<=n;i++)
    {

        cat=(cat+i+suma[1])%suma[1];
        if(cat==0)cat=suma[1];
        cautaBinar(1,1,n,cat);
        fout<<poz<<" ";
        val=0;
        cat--;
        update(1,1,n);
    }

    return 0;
}