#include // clang-format off using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair; const ll INF=4e18; #define debug1(a) { cerr<<#a<<":"< T random_choice(vector &vec) { return vec[engine() % vec.size()]; } bool anneal_accept(double new_score, double old_score, double cur_time, double begin_time, double end_time, double begin_temp, double end_temp) { const int ANNEAL_RND = 1e8; const double ANNEAL_EPS = 1e-6; double temp = (begin_temp * (end_time - cur_time) + end_temp * (cur_time - begin_time)) / (end_time - begin_time); return (exp((new_score - old_score) / temp) > double(engine() % ANNEAL_RND) / ANNEAL_RND + ANNEAL_EPS); } } // namespace marathon const int OPLIMIT = 50; const int N = 45; const ll F17 = 500000000000000000; card_t INIT_AB[N]; double evaluate(card_t x) { double da = abs(x.a - F17); double db = abs(x.b - F17); return max(da, db) * 100 + min(da, db); } double evaluate2(card_t x, card_t dest) { double da = abs(x.a - dest.a); double db = abs(x.b - dest.b); return max(da, db) * 100 + min(da, db); } void rec(vector &ops, vector &used, pair> &best_result, card_t used_total, card_t target, int lastind, int maxops, vector &now_cards) { if (ops.size() >= 1) { auto new_zero = avg(used_total, now_cards[lastind]); double e = evaluate(new_zero); if (e < best_result.first) { best_result = {e, ops}; } } if (ops.size() >= maxops) { return; } for (int j = 0; j < N; j++) { if (used[j]) continue; used[j] = true; ops.push_back(j); card_t nxt_used_total = (used_total.a <= 0) ? now_cards[j] : avg(now_cards[j], used_total); rec(ops, used, best_result, nxt_used_total, target, lastind, maxops, now_cards); ops.pop_back(); used[j] = false; } } void do_op(int i, int j, vector &now_cards, vector> &result) { card_t avg_ = avg(now_cards[i], now_cards[j]); now_cards[i] = avg_; now_cards[j] = avg_; result.push_back({i, j}); } void output(vector> &result) { cout << result.size() << endl; for (auto p : result) { cout << p.first + 1 << " " << p.second + 1 << endl; } } vector anneal(vector &now_cards) { vector ops; auto avgscore = [&]() { int m = ops.size(); ll a = 0; ll b = 0; if (ops.size() == 1) { return -evaluate(avg(now_cards[ops.back()], now_cards[0])); } else { for (int i = 0; i < m; i++) { if (i == m - 1) { a += now_cards[ops[i]].a / (2 << (m - 2)); b += now_cards[ops[i]].b / (2 << (m - 2)); } else { a += now_cards[ops[i]].a / (2 << i); b += now_cards[ops[i]].b / (2 << i); } } return -evaluate({(a + now_cards[0].a) / 2, (b + now_cards[0].b) / 2}); } }; { vector used(N); for (int it = 0; it < 25; it++) { pair best = {-1e50, -1}; for (int u = 1; u < N; u++) { if (used[u]) continue; ops.push_back(u); pair now = {avgscore(), u}; best = max(now, best); ops.pop_back(); } if (it < 10 || best.second >= 0) { used[best.second] = true; ops.push_back(best.second); } } } double ini_score = avgscore(); double old_score = ini_score; double begin_time = marathon::now(); double end_time = begin_time + 300; int anneal_iter = 0; int anneal_accepted = 0; pair> best = {ini_score, ops}; vector temps; double begin_temp = 1; double end_temp = 1; auto update_temps = [&](double _new_score) { }; // temps.push_back(abs(_new_score - old_score)); // int cnt = 0; // double sum = 0; // for (int it = max(0, int(temps.size()) - 10); it < temps.size(); it++) { // cnt++; // sum += temps[it]; // } // begin_temp = 0.0001 * sum / cnt; // end_temp = 0.1 * begin_temp; // }; while (marathon::now() < end_time) { anneal_iter++; int x = 1 + marathon::engine() % (N - 1); int y = 1 + marathon::engine() % (N - 1); if (x == y) continue; int xi = -1; int yi = -1; for (int i = 0; i < int(ops.size()); i++) { if (ops[i] == x) xi = i; if (ops[i] == y) yi = i; } if (xi < 0 && yi < 0) continue; if (xi < 0 && yi >= 0) { swap(x, y); swap(xi, yi); } if (xi >= 0 && yi < 0) { ops[xi] = y; double new_score = avgscore(); update_temps(new_score); // if (marathon::anneal_accept(new_score, old_score, marathon::now(), begin_time, end_time, begin_temp, end_temp)) { if (new_score >= old_score) { anneal_accepted++; old_score = new_score; } else { ops[xi] = x; } if (new_score > best.first) { best = {new_score, ops}; } } else { swap(ops[xi], ops[yi]); double new_score = avgscore(); update_temps(new_score); if (new_score >= old_score) { // if (marathon::anneal_accept(new_score, old_score, marathon::now(), begin_time, end_time, begin_temp, end_temp)) { anneal_accepted++; old_score = new_score; } else { swap(ops[xi], ops[yi]); } if (new_score > best.first) { best = {new_score, ops}; } } } debug4(ini_score, best.first, anneal_iter, anneal_accepted); return best.second; } void solve() { vector now_cards(N); for (int i = 0; i < N; i++) { now_cards[i] = INIT_AB[i]; } vector> result; while (result.size() + 25 <= OPLIMIT) { auto ops = anneal(now_cards); auto work_cards = now_cards; auto work_result = result; int m = ops.size(); for (int i = m - 1; i >= 1; i--) { do_op(ops[i], ops[i - 1], work_cards, work_result); } do_op(0, ops[0], work_cards, work_result); if (evaluate(work_cards[0]) < evaluate(now_cards[0])) { now_cards = work_cards; result = work_result; } else { break; } debug2(result.size(), now_cards[0]); } output(result); } int main() { marathon::marathon_init(); int n; cin >> n; for (int i = 0; i < n; i++) { ll a, b; cin >> a >> b; INIT_AB[i] = card_t{a, b}; } solve(); return 0; }