#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; } } struct state_t { card_t cards[N]; int ops[50]; bitset used; ll a; ll b; double score; double realscore; int ops_size; }; bool operator<(const state_t &lhs, const state_t &rhs) { return lhs.score < rhs.score; } void beam_swap(vector &stats, vector &nstats, int beamsize) { int nst_num = nstats.size(); vector perm(nst_num); iota(perm.begin(), perm.end(), 0); sort(perm.begin(), perm.end(), [&](int a, int b) { return nstats[a].score > nstats[b].score; }); unordered_set hashes; stats.clear(); for (int i = 0; i < nst_num; i++) { auto nst = nstats[perm[i]]; stats.push_back(nst); if (int(stats.size()) >= beamsize) { break; } } } vector beamsearch(int opnum, vector &now_cards) { state_t init_state; { init_state.used = 0; init_state.used[0] = 1; init_state.ops_size = 0; for (int u = 0; u < N; u++) { init_state.cards[u] = now_cards[u]; } init_state.a = 0; init_state.b = 0; init_state.score = -evaluate(now_cards[0]); init_state.realscore = -evaluate(now_cards[0]); } state_t beststate = init_state; int beamwidth = 800; vector stats = {init_state}; auto card0 = now_cards[0]; for (int op = 0; op < opnum; op++) { debug2(op, stats.size()); vector nstats; for (auto state : stats) { for (int i = 1; i < N; i++) { if (state.used[i]) continue; auto nst = state; ll da = now_cards[i].a / (2ll << op); ll db = now_cards[i].b / (2ll << op); nst.a += da; nst.b += db; ll df = F17 / (2ll << op); nst.score = -evaluate({(nst.a + df + card0.a) / 2, (nst.b + df + card0.b) / 2}); nst.realscore = -evaluate({(nst.a + da + card0.a) / 2, (nst.b + db + card0.b) / 2}); nst.ops[op] = i; nst.ops_size = op + 1; nst.used[i] = true; if (beststate.realscore < nst.realscore) { beststate = nst; } if ((nst.a + card0.a) / 2 > F17) continue; if ((nst.b + card0.b) / 2 > F17) continue; nstats.push_back(nst); } } debug2(stats.size(), nstats.size()); beam_swap(stats, nstats, beamwidth); } { vector ops; for (int i = 0; i < beststate.ops_size; i++) { ops.push_back(beststate.ops[i]); } return ops; } } void solve() { vector now_cards(N); for (int i = 0; i < N; i++) { now_cards[i] = INIT_AB[i]; } vector> result; int it = 0; { card_t init_0 = now_cards[0]; vector ops; vector used(N); pair> best_result = {evaluate(now_cards[0]) * 0.999, {}}; int maxop = min(4, OPLIMIT - 1 - int(result.size())); auto work_cards = now_cards; rec(ops, used, best_result, {0, 0}, init_0, 0, maxop, work_cards); for (int j = 0; j < int(best_result.second.size()) - 1; j++) { do_op(best_result.second[j], best_result.second[j + 1], now_cards, result); } do_op(best_result.second.back(), 0, now_cards, result); } debug2(evaluate(now_cards[0]), now_cards[0]); { auto ops = beamsearch(44, now_cards); int m = ops.size(); if (m > 0) { for (int i = m - 1; i >= 1; i--) { do_op(ops[i], ops[i - 1], now_cards, result); } do_op(0, ops[0], now_cards, result); } } debug2(evaluate(now_cards[0]), now_cards[0]); debug1(marathon::now()); 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; }