# Cod sursa(job #1469177)

Utilizator Data 7 august 2015 17:53:31 Order 90 cpp done Arhiva de probleme 2.3 kb
``````#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct Elimin {
vector<int> elim;
int n;

Elimin(int n) : n(n), elim(4 * n) { }

int m, i = 0, li = 1, lf = n;
while (li < lf) {
elim[i]++;
m = (li + lf) / 2;
if (pos <= m) {
i = 2 * i + 1;
lf = m;
} else {
i = 2 * i + 2;
li = m + 1;
}
}
elim[i]++;
}

int sol = 0;
int i = 0, li = 1, lf = n;
while (lf > b) {
int m = (li + lf) / 2;
if (m < b) {
sol += elim[2 * i + 1];
i = 2 * i + 2;
li = m + 1;
} else {
i = 2 * i + 1;
lf = m;
}
}

return sol + elim[i];
}

inline int wrap(int start, int tostep) {
if (start + tostep <= n) {
return start + tostep;
} else {
int sol = (tostep - (n - start)) % n;
return sol == 0 ? n : sol;
}
}

int ask_wrap(int start, int step) {
if (start + step <= n) {
//return ask(start + 1, start + step);
} else {
int full_laps = (step - (n - start)) / n;
int left = (step - (n - start)) % n;
return
full_laps * elim[0] +
(left > 0 ? ask1(left) : 0);
}
}

int find_advance(int start, int tostep) {
// We need to find a step such that step - ask_wrap(start, step) == tostep
int li = tostep;
int lf = n * (1 + tostep / (n - tostep + 1));
int minsol = 0, m;
while (li <= lf) {
m = (li + lf) / 2;
int realstep = m - ask_wrap(start, m);
//std::cerr << "realstep(" << start << " + " << m << ") = " << realstep << std::endl;
if (realstep == tostep) {
minsol = m;
lf = m - 1;
} else if (realstep > tostep) {
lf = m - 1;
} else {
li = m + 1;
}
}

//std::cerr << "Retuning " << wrap(start, minsol) << std::endl;
return wrap(start, minsol);
}
};

int n, p, step;

int main()
{
ifstream in("order.in");
in >> n;
in.close();
Elimin e(n);

ofstream out("order.out");
for (p = 1, step = 1; step <= n; ++step) {