Cod sursa(job #2028755)

Utilizator GoogalAbabei Daniel Googal Data 28 septembrie 2017 16:33:29
Problema Order Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NMAX = 30000 + 10;

int n;
int aib[1 + NMAX];

int bin_src(int x) {
  int step = 1 << 14;
  int res = 0;
  while(step != 0) {
    if((res ^ step) <= n && aib[(res ^ step)] < x) {
      res ^= step;
      x -= aib[res];
    }
  step /= 2;
  }

  return ++res;
}

void removeel(int x) {
  for(int i = x; i <= n; i += (i & -i))
    aib[i]--;
}

int main()
{
  in >> n;
  for(int i = 1; i <= n; i++)
    aib[i] = (i & (-1 * i));

  int curr = 2;
  for(int i = 1; i <= n; i++) {
    curr = (curr + i - 1) % (n - i + 1);
    if(curr <= 0)
      curr = n - i + 1;
    int pos = bin_src(curr);
    removeel(pos);
    out << pos << ' ';
  }

  in.close();
  out.close();
  return 0;
}