Submission #1253151

#TimeUsernameProblemLanguageResultExecution timeMemory
1253151BenqMigrations (IOI25_migrations)C++20
100 / 100
32 ms1296 KiB
#include "migrations.h" #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <vector> using namespace std; using ll = long long; using db = long double; // or double, if TL is tight using str = string; // yay python! // pairs using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; #define mp make_pair #define f first #define s second #define tcT template <class T #define tcTU tcT, class U // ^ lol this makes everything look weird but I'll try it tcT > using V = vector<T>; tcT, size_t SZ > using AR = array<T, SZ>; using vi = V<int>; using vb = V<bool>; using vl = V<ll>; using vd = V<db>; using vs = V<str>; using vpi = V<pi>; using vpl = V<pl>; using vpd = V<pd>; // vectors #define sz(x) int(size(x)) #define bg(x) begin(x) #define all(x) bg(x), end(x) #define rall(x) rbegin(x), rend(x) #define sor(x) sort(all(x)) #define rsz resize #define ins insert #define pb push_back #define eb emplace_back #define ft front() #define bk back() #define lb lower_bound #define ub upper_bound tcT > int lwb(const V<T> &a, const T &b) { return int(lb(all(a), b) - bg(a)); } tcT > int upb(const V<T> &a, const T &b) { return int(ub(all(a), b) - bg(a)); } // loops #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define F0R(i, a) FOR(i, 0, a) #define ROF(i, a, b) for (int i = (b) - 1; i >= (a); --i) #define R0F(i, a) ROF(i, 0, a) #define rep(a) F0R(_, a) #define each(a, x) for (auto &a : x) const int MOD = 998244353; // 1e9+7; const int MX = (int)2e5 + 5; const ll BIG = 1e18; // not too close to LLONG_MAX const db PI = acos((db)-1); const int dx[4]{1, 0, -1, 0}, dy[4]{0, 1, 0, -1}; // for every grid problem!! mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; // bitwise ops // also see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set constexpr int bits(int x) { // assert(x >= 0); // make C++11 compatible until // USACO updates ... return x == 0 ? 0 : 31 - __builtin_clz(x); } // floor(log2(x)) constexpr int p2(int x) { return 1 << x; } constexpr int msk2(int x) { return p2(x) - 1; } ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // divide a by b rounded down tcT > bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } // set a = min(a,b) tcT > bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } // set a = max(a,b) tcTU > T fstTrue(T lo, T hi, U f) { ++hi; assert(lo <= hi); // assuming f is increasing while (lo < hi) { // find first index such that f is true T mid = lo + (hi - lo) / 2; f(mid) ? hi = mid : lo = mid + 1; } return lo; } tcTU > T lstTrue(T lo, T hi, U f) { --lo; assert(lo <= hi); // assuming f is decreasing while (lo < hi) { // find first index such that f is true T mid = lo + (hi - lo + 1) / 2; f(mid) ? lo = mid : hi = mid - 1; } return lo; } tcT > void remDup(vector<T> &v) { // sort and remove duplicates sort(all(v)); v.erase(unique(all(v)), end(v)); } tcTU > void safeErase(T &t, const U &u) { auto it = t.find(u); assert(it != end(t)); t.erase(it); } inline namespace IO { #define SFINAE(x, ...) \ template <class, class = void> struct x : std::false_type {}; \ template <class T> struct x<T, std::void_t<__VA_ARGS__>> : std::true_type {} SFINAE(DefaultI, decltype(std::cin >> std::declval<T &>())); SFINAE(DefaultO, decltype(std::cout << std::declval<T &>())); SFINAE(IsTuple, typename std::tuple_size<T>::type); SFINAE(Iterable, decltype(std::begin(std::declval<T>()))); template <auto &is> struct Reader { template <class T> void Impl(T &t) { if constexpr (DefaultI<T>::value) is >> t; else if constexpr (Iterable<T>::value) { for (auto &x : t) Impl(x); } else if constexpr (IsTuple<T>::value) { std::apply([this](auto &...args) { (Impl(args), ...); }, t); } else static_assert(IsTuple<T>::value, "No matching type for read"); } template <class... Ts> void read(Ts &...ts) { ((Impl(ts)), ...); } }; template <class... Ts> void re(Ts &...ts) { Reader<cin>{}.read(ts...); } #define def(t, args...) \ t args; \ re(args); template <auto &os, bool debug, bool print_nd> struct Writer { string comma() const { return debug ? "," : ""; } template <class T> constexpr char Space(const T &) const { return print_nd && (Iterable<T>::value or IsTuple<T>::value) ? '\n' : ' '; } template <class T> void Impl(T const &t) const { if constexpr (DefaultO<T>::value) os << t; else if constexpr (Iterable<T>::value) { if (debug) os << '{'; int i = 0; for (auto &&x : t) ((i++) ? (os << comma() << Space(x), Impl(x)) : Impl(x)); if (debug) os << '}'; } else if constexpr (IsTuple<T>::value) { if (debug) os << '('; std::apply( [this](auto const &...args) { int i = 0; (((i++) ? (os << comma() << " ", Impl(args)) : Impl(args)), ...); }, t); if (debug) os << ')'; } else static_assert(IsTuple<T>::value, "No matching type for print"); } template <class T> void ImplWrapper(T const &t) const { if (debug) os << "\033[0;31m"; Impl(t); if (debug) os << "\033[0m"; } template <class... Ts> void print(Ts const &...ts) const { ((Impl(ts)), ...); } template <class F, class... Ts> void print_with_sep(const std::string &sep, F const &f, Ts const &...ts) const { ImplWrapper(f), ((os << sep, ImplWrapper(ts)), ...), os << '\n'; } void print_with_sep(const std::string &) const { os << '\n'; } }; template <class... Ts> void pr(Ts const &...ts) { Writer<cout, false, true>{}.print(ts...); } template <class... Ts> void ps(Ts const &...ts) { Writer<cout, false, true>{}.print_with_sep(" ", ts...); } } // namespace IO inline namespace Debug { template <typename... Args> void err(Args... args) { Writer<cerr, true, false>{}.print_with_sep(" | ", args...); } template <typename... Args> void errn(Args... args) { Writer<cerr, true, true>{}.print_with_sep(" | ", args...); } void err_prefix(str func, int line, string args) { cerr << "\033[0;31m\u001b[1mDEBUG\033[0m" << " | " << "\u001b[34m" << func << "\033[0m" << ":" << "\u001b[34m" << line << "\033[0m" << " - " << "[" << args << "] = "; } #ifdef LOCAL #define dbg(args...) err_prefix(__FUNCTION__, __LINE__, #args), err(args) #define dbgn(args...) err_prefix(__FUNCTION__, __LINE__, #args), errn(args) #else #define dbg(...) #define dbgn(args...) #endif const auto beg_time = std::chrono::high_resolution_clock::now(); // https://stackoverflow.com/questions/47980498/accurate-c-c-clock-on-a-multi-core-processor-with-auto-overclock?noredirect=1&lq=1 double time_elapsed() { return chrono::duration<double>(std::chrono::high_resolution_clock::now() - beg_time) .count(); } } // namespace Debug inline namespace FileIO { void setIn(str s) { freopen(s.c_str(), "r", stdin); } void setOut(str s) { freopen(s.c_str(), "w", stdout); } void setIO(str s = "") { cin.tie(0)->sync_with_stdio(0); // unsync C / C++ I/O streams cout << fixed << setprecision(12); // cin.exceptions(cin.failbit); // throws exception when do smth illegal // ex. try to read letter into int if (sz(s)) setIn(s + ".in"), setOut(s + ".out"); // for old USACO } } // namespace FileIO const int D = 14; V<AR<int, 14>> p; vi depth; AR<int, 2> diameter; AR<vi, 2> cands; int lca(int a, int b) { if (depth.at(a) < depth.at(b)) swap(a, b); R0F(d, D) if ((depth.at(a) - depth.at(b)) & (1 << d)) a = p.at(a).at(d); if (a == b) return a; R0F(d, D) if (p.at(a).at(d) != p.at(b).at(d)) { a = p.at(a).at(d); b = p.at(b).at(d); } return p.at(a).at(0); } int dist(int a, int b) { return depth.at(a) + depth.at(b) - 2 * depth.at(lca(a, b)); } int getpot(pi p, int cons = 1) { return (p.f + cons) * (p.s + cons); } template <class T> V<T> extract(V<T> v, int parts, int ret) { assert(sz(v)); while (sz(v) % parts) v.pb(v.bk); assert(0 <= ret && ret < parts); return V<T>(begin(v) + sz(v) / parts * ret, begin(v) + sz(v) / parts * (ret + 1)); } int idx(vi &v, int goal, int parts) { dbg("IDX", sz(v), v.ft, goal, parts); assert(sz(v)); while (sz(v) % parts) v.pb(v.bk); int i = 0; while (v.at(i) != goal) { ++i; assert(i < sz(v)); } int ret = i * parts / sz(v); v = extract(v, parts, ret); dbg("RET", ret); return ret; } // 10000, 182 (1 move) // 553, 25 (1 move) // 81, 6 (1 move) // 23, rest (6 moves) vi cuts; void init_cuts(int N) { cuts = {N}; // cuts[6] cuts.pb(cuts.bk - 3); // cuts[5] cuts.pb(cuts.bk - 2); // cuts[4] cuts.pb(cuts.bk - 2); // cuts[3] cuts.pb(cuts.bk - 6); // cuts[2] cuts.pb(cuts.bk - 20); // cuts[1] cuts.pb(cuts.bk - 140); // cuts[0] reverse(all(cuts)); assert(sz(cuts) == 7); } pi pos_val; // full credit vi with_label; const V<V<AR<int, 2>>> partitions{{{0, 4}, {0, 5}, {0, 6}, {0, 8}, {8, 4}}, {{0, 7}, {1, 7}, {1, 4}, {1, 8}, {8, 7}}, {{1, 5}, {1, 6}, {2, 6}, {2, 8}, {8, 6}}, {{2, 4}, {2, 5}, {3, 5}, {3, 8}, {8, 5}}, {{3, 4}, {3, 6}, {3, 7}, {2, 7}}}; V<AR<int, 2>> cand_pairs; bool common(AR<int, 2> a, AR<int, 2> b) { each(x, a) each(y, b) if (x == y) return true; return false; } V<AR<int, 2>> add_all(V<AR<int, 2>> v, int nvert) { V<AR<int, 2>> cands = v; each(t, v) { cands.pb({t[0], nvert}); cands.pb({t[1], nvert}); } remDup(cands); return cands; } V<AR<int, 2>> valid_ordering(V<AR<int, 2>> v) { sor(v); do { bool ok = true; for (int i = 0; i + 1 < sz(v); i += 2) { if (!common(v[i], v[i + 1])) { ok = false; break; } } if (ok) return v; } while (next_permutation(all(v))); assert(false); } AR<int, 2> pair_to_edge(AR<int, 2> t) { return {with_label.at(t[0]), with_label.at(t[1])}; } bool edge_eq(AR<int, 2> a, AR<int, 2> b) { if (a == b) return true; if (a == AR<int, 2>{b[1], b[0]}) return true; return false; } int c2(int x) { return x * (x + 1) / 2; }; int send_message(int N, int i, int Pi) { if (i == 1) { p.eb(), depth.eb(); F0R(j, 2) cands.at(j).pb(0); init_cuts(N); } p.eb(); p.bk.at(0) = Pi; depth.pb(depth.at(Pi) + 1); FOR(d, 1, D) p.bk.at(d) = p.at(p.bk.at(d - 1)).at(d - 1); assert(sz(p) == i + 1); F0R(j, 2) { if (dist(i, diameter.at(j)) > dist(diameter.at(0), diameter.at(1))) { diameter.at(j ^ 1) = i; break; } } int cut_idx = 0; while (cuts.at(cut_idx + 1) <= i) ++cut_idx; if (cut_idx > 0 && cuts.at(cut_idx) == i) { FOR(j, cuts.at(cut_idx - 1) + 1, cuts.at(cut_idx)) { cands.at(0).pb(j); cands.at(1).pb(j); } } int msg = 0; if (i >= cuts.at(5)) { if (i == cuts.at(5)) { dbg("e phase 2", i, sz(cands.at(0))); assert(sz(cands.at(0)) == 4 && sz(cands.at(1)) == 4); with_label = {}; with_label.ins(end(with_label), all(cands.at(0))); with_label.ins(end(with_label), all(cands.at(1))); with_label.pb(i); while (true) { bool found = 0; each(t, partitions.at(msg)) { if (diameter == pair_to_edge(t)) { found = true; } } if (found) break; ++msg; } cand_pairs = partitions.at(msg); } else if (i == cuts.at(5) + 1) { dbg("e phase 3", cand_pairs); with_label.pb(i); cand_pairs = add_all(cand_pairs, 9); cand_pairs = valid_ordering(cand_pairs); while (sz(cand_pairs) < 10) cand_pairs.pb(cand_pairs.bk); assert(sz(cand_pairs) == 10); int ret = 0; while (!edge_eq(diameter, pair_to_edge(cand_pairs.at(ret)))) { ++ret; } msg = ret / 2; cand_pairs = extract(cand_pairs, 5, msg); } else { dbg("e phase 4", cand_pairs); with_label.pb(i); assert(i == cuts.at(5) + 2); cand_pairs = add_all(cand_pairs, 10); assert(sz(cand_pairs) <= 5); while (!edge_eq(diameter, pair_to_edge(cand_pairs.at(msg)))) ++msg; dbg(diameter); } } else if (i >= cuts.at(0)) { if (i == cuts.at(cut_idx)) { cands.at(0).pb(i); cands.at(1).pb(i); dbg("phase 1", i, sz(cands.at(0)), sz(cands.at(1))); assert(sz(cands.at(0)) == sz(cands.at(1))); const int space = cuts.at(cut_idx + 1) - cuts.at(cut_idx); const int num_cands = 4 * space + 1; int to_encrypt = 0; if (cut_idx == 0) { int sqrt_cands = 1; while (c2(sqrt_cands + 1) <= num_cands) ++sqrt_cands; if (diameter.at(0) < diameter.at(1)) swap(diameter.at(0), diameter.at(1)); to_encrypt = c2(idx(cands.at(0), diameter.at(0), sqrt_cands)) + idx(cands.at(1), diameter.at(1), sqrt_cands); } else { int sqrt_cands = sqrt(num_cands); F0R(j, 2) { to_encrypt = sqrt_cands * to_encrypt + idx(cands.at(j), diameter.at(j), sqrt_cands); } } assert(0 <= to_encrypt && to_encrypt < num_cands); dbg(i, to_encrypt); if (to_encrypt == 4 * space) { pos_val = {-1, -1}; } else { pos_val = {i + to_encrypt / 4, 1 + to_encrypt % 4}; } } if (i == pos_val.f) { assert(1 <= pos_val.s && pos_val.s <= 4); msg = pos_val.s; } } else { F0R(j, 2) cands.at(j).pb(i); } return msg; } pi arr_to_pair(AR<int, 2> a) { return {a[0], a[1]}; } std::pair<int, int> longest_path(std::vector<int> S) { p = {}; depth = {}; diameter = {}; cands = {}; int N = sz(S); init_cuts(N); F0R(i, N) { // dbg("AT", i); int cut_idx = 0; while (cuts.at(cut_idx + 1) <= i) ++cut_idx; if (cut_idx > 0 && cuts.at(cut_idx) == i) { int space = cuts.at(cut_idx) - cuts.at(cut_idx - 1); int num_cands = 4 * space + 1; int sqrt_cands = 0; if (cut_idx == 1) { while (c2(sqrt_cands + 1) <= num_cands) ++sqrt_cands; } else { sqrt_cands = sqrt(num_cands); } int to_encrypt = 0; if (pos_val == mp(-1, -1)) to_encrypt = 4 * space; else { to_encrypt = (pos_val.f - cuts.at(cut_idx - 1)) * 4 + (pos_val.s - 1); } assert(0 <= to_encrypt && to_encrypt < num_cands); AR<int, 2> idx{}; if (cut_idx == 1) { while (c2(idx.at(0) + 1) <= to_encrypt) ++idx.at(0); idx.at(1) = to_encrypt - c2(idx.at(0)); } else { idx = {to_encrypt / sqrt_cands, to_encrypt % sqrt_cands}; } dbg("rextract", cuts.at(cut_idx - 1), pos_val, to_encrypt, cut_idx, sqrt_cands, idx); F0R(j, 2) cands.at(j) = extract(cands.at(j), sqrt_cands, idx.at(j)); FOR(j, cuts.at(cut_idx - 1) + 1, cuts.at(cut_idx)) { cands.at(0).pb(j); cands.at(1).pb(j); } dbg("DONE"); } int msg = S.at(i); if (i >= cuts.at(5)) { if (i == cuts.at(5)) { dbg("d phase 2", i, sz(cands.at(0))); dbg(cands.at(0), cands.at(1)); assert(sz(cands.at(0)) == 4 && sz(cands.at(1)) == 4); with_label = {}; with_label.ins(end(with_label), all(cands.at(0))); with_label.ins(end(with_label), all(cands.at(1))); with_label.pb(i); cand_pairs = partitions.at(msg); } else if (i == cuts.at(5) + 1) { dbg("d phase 3", i, cand_pairs); with_label.pb(i); cand_pairs = add_all(cand_pairs, 9); cand_pairs = valid_ordering(cand_pairs); while (sz(cand_pairs) < 10) cand_pairs.pb(cand_pairs.bk); assert(sz(cand_pairs) == 10); cand_pairs = extract(cand_pairs, 5, msg); } else { dbg("d phase 4", i, cand_pairs); assert(i == cuts.at(5) + 2); with_label.pb(i); assert(i == cuts.at(5) + 2); cand_pairs = add_all(cand_pairs, 10); assert(sz(cand_pairs) <= 5); return arr_to_pair(pair_to_edge(cand_pairs.at(msg))); } } else if (i >= cuts.at(0)) { if (i == cuts.at(cut_idx)) { pos_val = {-1, -1}; cands.at(0).pb(i); cands.at(1).pb(i); } if (msg != 0) { assert(pos_val == mp(-1, -1)); pos_val = {i, msg}; } } else { assert(msg == 0); F0R(j, 2) cands.at(j).pb(i); } if (i == N - 1) { F0R(j, 2) assert(sz(cands.at(j)) == 1); return {cands.at(0).ft, cands.at(1).ft}; } } assert(false); }

Compilation message (stderr)

migrations.cpp: In function 'void FileIO::setIn(str)':
migrations.cpp:248:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  248 | void setIn(str s) { freopen(s.c_str(), "r", stdin); }
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
migrations.cpp: In function 'void FileIO::setOut(str)':
migrations.cpp:249:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  249 | void setOut(str s) { freopen(s.c_str(), "w", stdout); }
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...