#ifndef HITONANODE_LOCAL // #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include class JsonDumper { struct KeyValue { std::string key; std::string value; }; std::vector _items; bool dump_at_end = false; public: JsonDumper(bool dump_at_end_ = false) : dump_at_end(dump_at_end_) {} ~JsonDumper() { if (dump_at_end) std::cout << dump() << std::endl; } void set_dump_at_end() { dump_at_end = true; } void operator()(const std::string &key, const std::string &value) { _items.push_back(KeyValue{key, "\"" + value + "\""}); } template void operator()(const std::string &key, T value) { _items.push_back(KeyValue{key, std::to_string(value)}); } std::string dump() const { std::string ret = "{\n"; if (!_items.empty()) { for (const auto &[k, v] : _items) ret += " \"" + k + "\": " + v + ",\n"; ret.erase(ret.end() - 2); } ret += "}"; return ret; } } jdump; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); } template std::pair operator+(const std::pair &l, const std::pair &r) { return std::make_pair(l.first + r.first, l.second + r.second); } template std::pair operator-(const std::pair &l, const std::pair &r) { return std::make_pair(l.first - r.first, l.second - r.second); } template std::vector sort_unique(std::vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template IStream &operator>>(IStream &is, std::vector &vec) { for (auto &v : vec) is >> v; return is; } template OStream &operator<<(OStream &os, const std::vector &vec); template OStream &operator<<(OStream &os, const std::array &arr); template OStream &operator<<(OStream &os, const std::unordered_set &vec); template OStream &operator<<(OStream &os, const std::pair &pa); template OStream &operator<<(OStream &os, const std::deque &vec); template OStream &operator<<(OStream &os, const std::set &vec); template OStream &operator<<(OStream &os, const std::multiset &vec); template OStream &operator<<(OStream &os, const std::unordered_multiset &vec); template OStream &operator<<(OStream &os, const std::pair &pa); template OStream &operator<<(OStream &os, const std::map &mp); template OStream &operator<<(OStream &os, const std::unordered_map &mp); template OStream &operator<<(OStream &os, const std::tuple &tpl); template OStream &operator<<(OStream &os, const std::vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } template std::istream &operator>>(std::istream &is, std::tuple &tpl) { std::apply([&is](auto &&...args) { ((is >> args), ...); }, tpl); return is; } template OStream &operator<<(OStream &os, const std::tuple &tpl) { os << '('; std::apply([&os](auto &&...args) { ((os << args << ','), ...); }, tpl); return os << ')'; } template OStream &operator<<(OStream &os, const std::unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::pair &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; } template OStream &operator<<(OStream &os, const std::map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const std::string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) \ std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " \ << __FILE__ << COLOR_RESET << std::endl #define dbgif(cond, x) \ ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ \ << ") " << __FILE__ << COLOR_RESET << std::endl \ : std::cerr) #else #define dbg(x) 0 #define dbgif(cond, x) 0 #endif #ifdef BENCHMARK #define dump_onlinejudge(x) 0 struct setenv { setenv() { jdump.set_dump_at_end(); } } setenv_; #else #define dump_onlinejudge(x) (std::cout << (x) << std::endl) #endif #include #include #include #include #include using namespace std; using lint = long long; using pint = std::pair; using plint = std::pair; struct fast_ios { fast_ios() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(20); }; } fast_ios_; constexpr int N = 45; // void gen constexpr lint AMIN = 100000000000000000; constexpr lint AMAX = 1000000000000000000; constexpr lint goalx = 500000000000000000; constexpr lint goaly = 500000000000000000; constexpr int MAXT = 50; #include #include uint32_t rand_int() // XorShift random integer generator { static uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123; uint32_t t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } double rand_double() { return (double)rand_int() / UINT32_MAX; } template void shuffle_vec(std::vector &vec) { for (int i = 1; i < (int)vec.size(); ++i) { const int j = rand_int() % (i + 1); std::swap(vec.at(i), vec.at(j)); } } #include #include struct rand_int_ { using lint = long long; std::mt19937 mt; rand_int_() : mt(std::chrono::steady_clock::now().time_since_epoch().count()) {} lint operator()(lint x) { return this->operator()(0, x); } // [0, x) lint operator()(lint l, lint r) { std::uniform_int_distribution d(l, r - 1); return d(mt); } } rnd; double eval_diversity(const array &A, const array &B, const bitset &alives, lint cx, lint cy) { const int Z = 60; // const double r = 0.9; const double dec = 2.0; vector hi(Z, -1e18); vector invdsum(Z); REP(i, A.size()) { if (!alives[i]) continue; const lint a = A.at(i); const lint b = B.at(i); const double theta = atan2(b - cy, a - cx); int z = (theta / (2 * M_PI) + 0.5) * Z; // z = (z % Z + Z) % Z; const double dist = max(abs(a - cx), abs(b - cy)) + 1; // chmax(hi.at(z), -log10(dist)); invdsum.at(z) += 1.0 / dist; } REP(z, Z) { if (invdsum.at(z)) hi.at(z) = log10(invdsum.at(z)); } REP(_, 2) { FOR(i, 1, Z) { chmax(hi.at(i), hi.at(i - 1) - dec); } chmax(hi.at(0), hi.at(Z - 1) - dec); IFOR(i, 1, Z) { chmax(hi.at(i - 1), hi.at(i) - dec); } chmax(hi.at(Z - 1), hi.at(0) - dec); } return accumulate(ALL(hi), 0.0); } int calc_score(lint max_v1v2) { if (!max_v1v2) return 2000000 + 50; double sol = 2000000 - 100000 * log10(max_v1v2 + 1); return floor(sol); } using SolutionState = vector; double eval_l2(const array &Ainit, const array &Binit, const SolutionState &sol) { array A = Ainit, B = Binit; for (auto [i, j] : sol) { lint tmpa = (A.at(i) + A.at(j)) / 2; lint tmpb = (B.at(i) + B.at(j)) / 2; A.at(i) = A.at(j) = tmpa; B.at(i) = B.at(j) = tmpb; } const double dx = abs(A.at(0) - goalx), dy = abs(B.at(0) - goaly); return sqrt(dx * dx + dy * dy); } lint eval_linf(const array &Ainit, const array &Binit, const SolutionState &sol) { array A = Ainit, B = Binit; for (auto [i, j] : sol) { lint tmpa = (A.at(i) + A.at(j)) / 2; lint tmpb = (B.at(i) + B.at(j)) / 2; A.at(i) = A.at(j) = tmpa; B.at(i) = B.at(j) = tmpb; } const lint dx = abs(A.at(0) - goalx), dy = abs(B.at(0) - goaly); return max(dx, dy); } template struct ExponentialDistSampler { std::array logps; constexpr ExponentialDistSampler() { for (int i = 0; i < (1 << D); ++i) logps.at(i) = log((0.5 + i) / (1 << D)); } double sample(uint32_t p) const { return logps.at(p & ((1 << D) - 1)); } // exp(-|dx| / T) >= p <=> -|dx| >= log(p) * T, p ~ U(0, 1) bool operator()(double abs_dx, double T, uint32_t p) const { return -abs_dx >= logps.at(p & ((1 << D) - 1)) * T; } }; const ExponentialDistSampler<16> log_ps; struct Solver { array initA, initB; vector intro_ops; vector best_intro_ops; vector after_intro; vector best_after_intro; lint best_cost = 1LL << 62; void check_best() { if (chmin(best_cost, cost)) { best_intro_ops = intro_ops; best_after_intro = after_intro; } } array preprocessedA, preprocessedB; long long gx2, gy2; lint cost; static constexpr int E = 1; void precalc() { preprocessedA = initA; preprocessedB = initB; for (auto [i, j] : intro_ops) { lint tmpa = (preprocessedA.at(i) + preprocessedA.at(j)) / 2; lint tmpb = (preprocessedB.at(i) + preprocessedB.at(j)) / 2; preprocessedA.at(i) = preprocessedA.at(j) = tmpa; preprocessedB.at(i) = preprocessedB.at(j) = tmpb; } gx2 = preprocessedA.at(0) >> (after_intro.size() - E); gy2 = preprocessedB.at(0) >> (after_intro.size() - E); REP(d, after_intro.size()) { const int i = after_intro.at(d); gx2 += preprocessedA.at(i) >> (after_intro.size() - d - E); gy2 += preprocessedB.at(i) >> (after_intro.size() - d - E); } cost = max(abs((gx2 >> E) - goalx), abs((gy2 >> E) - goaly)); check_best(); } Solver(const array &A, const array &B) : initA(A), initB(B) { after_intro.resize(N - 1); REP(i, N - 1) after_intro.at(i) = i + 1; precalc(); } bool swap2(int d, int e, double temp) { const int i = after_intro.at(d); const int j = after_intro.at(e); lint tmpgx2 = gx2 - (preprocessedA.at(i) >> (after_intro.size() - d - E)) + (preprocessedA.at(j) >> (after_intro.size() - d - E)); tmpgx2 += (preprocessedA.at(i) >> (after_intro.size() - e - E)) - (preprocessedA.at(j) >> (after_intro.size() - e - E)); lint tmpgy2 = gy2 - (preprocessedB.at(i) >> (after_intro.size() - d - E)) + (preprocessedB.at(j) >> (after_intro.size() - d - E)); tmpgy2 += (preprocessedB.at(i) >> (after_intro.size() - e - E)) - (preprocessedB.at(j) >> (after_intro.size() - e - E)); lint tmpcost = max(abs((tmpgx2 >> E) - goalx), abs((tmpgy2 >> E) - goaly)); if (tmpcost <= cost or log_ps(log(1.0 * tmpcost / cost), temp, rand_int())) { swap(after_intro.at(d), after_intro.at(e)); gx2 = tmpgx2; gy2 = tmpgy2; cost = tmpcost; check_best(); return true; } else { return false; } } }; // exp(-log10(tmpcost / cost) / temp) >= p // -log10(tmpcost / cost) / temp >= log(p) // - log(tmpcost / cost) >= log(p) * log(10) * temp int main(int argc, char *argv[]) { { int n_; cin >> n_; assert(N == n_); } array A, B; { for (int i = 0; i < N; i++) { cin >> A.at(i) >> B.at(i); } } Solver solver(A, B); // auto A0 = A, B0 = B; // REP(_, 6) { // lint maxdiff = 0; // int a = 0, b = 0; // REP(i, N) REP(j, i) { // lint diff = max(abs(A0.at(i) - A0.at(j)), abs(B0.at(i) - B0.at(j))); // if (chmax(maxdiff, diff)) { // a = i, b = j; // } // } // solver.intro_ops.emplace_back(a, b); // lint tmpa = (A0.at(a) + A0.at(b)) / 2; // lint tmpb = (B0.at(a) + B0.at(b)) / 2; // A0.at(a) = A0.at(b) = tmpa; // B0.at(a) = B0.at(b) = tmpb; // } // solver.precalc(); const double Tbegin = 1 * log(10), Tend = 0.01 * log(10); constexpr int L = 10000000 * 2 / 10; // const double rT = pow(Tend / Tbegin, 1.0 / L); const double dT = (Tend - Tbegin) / L; REP(_, 10) { double T = Tbegin; REP(_, L) { // T *= rT; T += dT; int a = 0, b = 0; if (rand_double() < 0.2) { while (a == b) { a = rand_int() % (N - 1); b = rand_int() % (N - 1); } } else { const int s = rand_int() % 3 + 1; a = rand_int() % (N - 1 - s); b = a + s; } solver.swap2(a, b, T); } dbg(make_tuple(_, solver.cost, solver.best_cost)); } vector best = solver.best_intro_ops; for (int i : solver.best_after_intro) { best.emplace_back(0, i); } dump_onlinejudge(best.size()); for (auto [i, j] : best) { dump_onlinejudge(to_string(i + 1) + " " + to_string(j + 1)); } const lint cost = eval_linf(A, B, best); const int score = calc_score(cost); jdump("score", score); dbg(cost); dbg(score); }