#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 { int id; bitset used; ll a; ll b; double score; double realscore; }; uint8_t stateid_to_cardid[10000000]; int stateid_to_preid[10000000]; int stateid_size = 0; void add_state(state_t &pre, state_t &nxt, uint8_t card_id) { nxt.id = stateid_size; stateid_to_cardid[stateid_size] = card_id; stateid_to_preid[stateid_size] = pre.id; stateid_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.id = 0; stateid_size = 1; init_state.a = now_cards[0].a / 2; init_state.b = now_cards[0].b / 2; init_state.score = -1e18; init_state.realscore = -1e18; } state_t beststate = init_state; int beamwidth = 4000; vector stats = {init_state}; auto card0 = now_cards[0]; for (int op = 1; op <= opnum; op++) { debug3(op, stats.size(),stateid_size); vector nstats; for (auto state : stats) { for (int card_id = 1; card_id < N; card_id++) { if (state.used[card_id]) continue; auto nst = state; ll da = now_cards[card_id].a / (2ll << op); ll db = now_cards[card_id].b / (2ll << op); nst.a += da; nst.b += db; ll df = F17 / (2ll << op); nst.score = -evaluate({nst.a + df, nst.b + df}); nst.realscore = -evaluate({nst.a + da, nst.b + db}); nst.used[card_id] = true; add_state(state, nst, card_id); if (beststate.realscore < nst.realscore) { beststate = nst; } if (nst.a > F17 + 100000) continue; if (nst.b > F17 + 100000) continue; nstats.push_back(nst); } } debug2(stats.size(), nstats.size()); beam_swap(stats, nstats, beamwidth); } { vector ops; int state_id = beststate.id; while (state_id > 0) { ops.push_back(stateid_to_cardid[state_id]); state_id = stateid_to_preid[state_id]; } reverse(ops.begin(), ops.end()); 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; }