Cod sursa(job #3376726)

Utilizator Daniel150212daniel munteanu Daniel150212 Data 19 noiembrie 2026 09:26:47
Problema Order Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
#define int long long
using namespace std;
ifstream cin("order.in");
ofstream cout("order.out");
const int MAXN=200005;
int aib[MAXN];
int v[MAXN];
int n;
void update(int poz, int val){
    while(poz<=n){
        aib[poz]+=val;
        poz+=poz&(-poz);
    }
}
signed main()
{
    int q;
    cin>>n;
    for(int i=1; i<=n; i++){
        update(i, 1);
    }
    int c=2;
    int mod=n;
    for(int i=1; i<=n; i++){
        c=(c+i-1)%mod;
        if(c==0) c=mod;
        int ci=c;
        mod--;
        int poz=0;
        for(int p=1<<18; p>=1; p/=2){
            int urm=poz+p;
            if(urm<=n && aib[urm]<c){
                poz=urm;
                c-=aib[urm];
            }
        }
        c=ci;
        poz++;
        cout<<poz<<" ";
        update(poz, -1);
    }
}