Cod sursa(job #1254002)

Utilizator DenisacheDenis Ehorovici Denisache Data 2 noiembrie 2014 01:29:29
Problema Order Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.78 kb
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
//#include <windows.h>
//#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <limits.h>
#include <string>
#include <math.h>
#include <iomanip>
using namespace std;
#define forA(V,it) for (typeof(V.begin()) it=V.begin();it!=V.end();it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define ull unsigned ll
#define MOD 1000000007
#define INF (1<<31)-1
#define MINF -(1<<31)
#define vi vector <int>
#define vll vector <ll>
#define pii pair <int,int>
#define pll pair <ll,ll>
#define newl printf("\n")
#define DIM (1<<16)
int arbint[DIM+5],ind,N;
void build(int nod,int st,int dr)
{
    arbint[nod]=dr-st+1;
    if (st==dr) return;
    int mid=(st+dr)>>1;
    build(nod<<1,st,mid);
    build((nod<<1)+1,mid+1,dr);
}
int query(int nod,int st,int dr,int poz)
{
    if (st==dr) return st;
    int mid=(st+dr)>>1;
    if (poz<=arbint[nod<<1]) return query(nod<<1,st,mid,poz);
    else return query((nod<<1)+1,mid+1,dr,poz-arbint[nod<<1]);
}
void update(int nod,int st,int dr,int x)
{
    arbint[nod]--;
    if (st==dr) return;
    int mid=(st+dr)>>1;
    if (x<=mid) update(nod<<1,st,mid,x);
    else update((nod<<1)+1,mid+1,dr,x);
}
int main()
{
    freopen("order.in","r",stdin);
    freopen("order.out","w",stdout);
    scanf("%d",&N);
    build(1,1,N);
    int poz=2,step=1;
    while (arbint[1])
    {
        poz=(poz+step-1)%arbint[1];
        if (poz==0) poz=arbint[1];
        int elem=query(1,1,N,poz);
        printf("%d ",elem);
        update(1,1,N,elem);
        step++;
    }
    return 0;
}