#define _CRT_NONSTDC_NO_WARNINGS #define _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING #include #include #include #include #include #ifdef _MSC_VER #include #include #include #include #include #include #include #include /* g++ functions */ int __builtin_clz(unsigned int n) { unsigned long index; _BitScanReverse(&index, n); return 31 - index; } int __builtin_ctz(unsigned int n) { unsigned long index; _BitScanForward(&index, n); return index; } namespace std { inline int __lg(int __n) { return sizeof(int) * 8 - 1 - __builtin_clz(__n); } } /* enable __uint128_t in MSVC */ //#include //using __uint128_t = boost::multiprecision::uint128_t; #else #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif /** compro io **/ namespace aux { template struct tp { static void output(std::ostream& os, const T& v) { os << std::get(v) << ", "; tp::output(os, v); } }; template struct tp { static void output(std::ostream& os, const T& v) { os << std::get(v); } }; } template std::ostream& operator<<(std::ostream& os, const std::tuple& t) { os << '{'; aux::tp, 0, sizeof...(Ts) - 1>::output(os, t); return os << '}'; } // tuple out template std::basic_ostream& operator<<(std::basic_ostream& os, const Container& x); // container out (fwd decl) template std::ostream& operator<<(std::ostream& os, const std::pair& p) { return os << '{' << p.first << ", " << p.second << '}'; } // pair out template std::istream& operator>>(std::istream& is, std::pair& p) { return is >> p.first >> p.second; } // pair in std::ostream& operator<<(std::ostream& os, const std::vector::reference& v) { os << (v ? '1' : '0'); return os; } // bool (vector) out std::ostream& operator<<(std::ostream& os, const std::vector& v) { bool f = true; os << '{'; for (const auto& x : v) { os << (f ? "" : ", ") << x; f = false; } os << '}'; return os; } // vector out template std::basic_ostream& operator<<(std::basic_ostream& os, const Container& x) { bool f = true; os << '{'; for (auto& y : x) { os << (f ? "" : ", ") << y; f = false; } return os << '}'; } // container out template())), class = typename std::enable_if::value>::type> std::istream& operator>>(std::istream& is, T& a) { for (auto& x : a) is >> x; return is; } // container in template auto operator<<(std::ostream& out, const T& t) -> decltype(out << t.stringify()) { out << t.stringify(); return out; } // struct (has stringify() func) out /** io setup **/ struct IOSetup { IOSetup(bool f) { if (f) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } std::cout << std::fixed << std::setprecision(15); } } iosetup(true); // set false when solving interective problems /** string formatter **/ template std::string format(const std::string& f, Ts... t) { size_t l = std::snprintf(nullptr, 0, f.c_str(), t...); std::vector b(l + 1); std::snprintf(&b[0], l + 1, f.c_str(), t...); return std::string(&b[0], &b[0] + l); } /** dump **/ #define DUMPOUT std::cerr std::ostringstream DUMPBUF; #define dump(...) do{DUMPBUF<<" ";DUMPBUF<<#__VA_ARGS__<<" :[DUMP - "<<__LINE__<<":"<<__FUNCTION__<<']'< void dump_func(Head&& head, Tail&&... tail) { DUMPBUF << head; if (sizeof...(Tail) == 0) { DUMPBUF << " "; } else { DUMPBUF << ", "; } dump_func(std::move(tail)...); } /** timer **/ class Timer { double t = 0, paused = 0, tmp; public: Timer() { reset(); } static double time() { #ifdef _MSC_VER return __rdtsc() / 2.3e9; #else unsigned long long a, d; __asm__ volatile("rdtsc" : "=a"(a), "=d"(d)); return (d << 32 | a) / 2.3e9; #endif } void reset() { t = time(); } void pause() { tmp = time(); } void restart() { paused += time() - tmp; } double elapsed_ms() const { return (time() - t - paused) * 1000.0; } }; /** rand **/ struct Xorshift { static constexpr uint64_t M = INT_MAX; static constexpr double e = 1.0 / M; uint64_t x = 88172645463325252LL; Xorshift() {} Xorshift(uint64_t seed) { reseed(seed); } inline void reseed(uint64_t seed) { x = 0x498b3bc5 ^ seed; for (int i = 0; i < 20; i++) next(); } inline uint64_t next() { x = x ^ (x << 7); return x = x ^ (x >> 9); } inline int next_int() { return next() & M; } inline int next_int(int mod) { return next() % mod; } inline int next_int(int l, int r) { return l + next_int(r - l + 1); } inline double next_double() { return next_int() * e; } }; /** shuffle **/ template void shuffle_vector(std::vector& v, Xorshift& rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_int(i); std::swap(v[i], v[r]); } } /** split **/ std::vector split(const std::string& str, const std::string& delim) { std::vector res; std::string buf; for (const auto& c : str) { if (delim.find(c) != std::string::npos) { if (!buf.empty()) res.push_back(buf); buf.clear(); } else buf += c; } if (!buf.empty()) res.push_back(buf); return res; } /** misc **/ template inline void Fill(A(&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); } // fill array template auto make_vector(T x, int arg, Args ...args) { if constexpr (sizeof...(args) == 0)return std::vector(arg, x); else return std::vector(arg, make_vector(x, args...)); } template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } /** using **/ using ll = long long; using ld = long double; //using ld = boost::multiprecision::cpp_bin_float_quad; using pii = std::pair; using pll = std::pair; using std::cin, std::cout, std::cerr, std::endl, std::string, std::vector, std::array; constexpr int N = 100; constexpr int M = 8; constexpr int alpha = 5; int xs[N + M + 1]; int ys[N + M + 1]; int C[N + M + 1][N + M + 1]; int ps[N + 1]; void initialize(std::istream& in) { int buf; in >> buf >> buf; // ignore N, M for (int i = 1; i <= N; i++) { in >> xs[i] >> ys[i]; } for (int u = 1; u + 1 <= N; u++) { for (int v = u + 1; v <= N; v++) { int d2 = (xs[u] - xs[v]) * (xs[u] - xs[v]) + (ys[u] - ys[v]) * (ys[u] - ys[v]); C[u][v] = C[v][u] = d2 * alpha * alpha; } } } int calc_cost() { int cost = 0; for (int i = 0; i < N; i++) { cost += C[ps[i]][ps[i + 1]]; } return cost; } int calc_diff(int i, int j) { int u1 = ps[i], u2 = ps[i + 1], v1 = ps[j], v2 = ps[j + 1]; return C[u1][v1] + C[u2][v2] - C[u1][u2] - C[v1][v2]; } int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) { Timer timer; #ifdef HAVE_OPENCV_HIGHGUI cv::utils::logging::setLogLevel(cv::utils::logging::LogLevel::LOG_LEVEL_SILENT); #endif #if 0 std::ifstream ifs("../../tools/in/0000.txt"); std::istream& in = ifs; std::ofstream ofs("../../tools/out/0000.txt"); std::ostream& out = ofs; #else std::istream& in = cin; std::ostream& out = cout; #endif initialize(in); for (int i = 0; i < N; i++) { ps[i] = i + 1; } ps[N] = 1; Xorshift rnd; int cost = calc_cost(); while (timer.elapsed_ms() < 950) { int i = rnd.next_int(N); int j = rnd.next_int(N - 1); j += (i == j); if (i > j) std::swap(i, j); int diff = calc_diff(i, j); if (diff < 0) { std::reverse(ps + i + 1, ps + j + 1); cost += diff; } } for (int i = 0; i < M; i++) { out << "0 0\n"; } out << N + 1 << '\n'; for (int i = 0; i < N; i++) { out << 1 << ' ' << ps[i] << '\n'; } out << "1 1" << '\n'; return 0; }