Submission #1251099

#TimeUsernameProblemLanguageResultExecution timeMemory
1251099BenqTriple Peaks (IOI25_triples)C++20
100 / 100
1358 ms64840 KiB
#include "triples.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(0); 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 long long count_triples(std::vector<int> H) { // a, b // a, b, a + b // a, a + b, b // b, a + b, a (special) // b, a, a + b // a + b, a, b // a + b, b, a int N = sz(H); set<AR<int, 3>> cands; auto try_triple = [&](int x, int y, int z) { if (!(x < y && y < z)) return; if (!(0 <= x && z < N)) return; vi difs{y - x, z - y, z - x}; vi expected{H.at(x), H.at(y), H.at(z)}; sor(difs); sor(expected); if (difs != expected) return; if (H.at(x) == z - y && H.at(z) == y - x) return; cands.ins({x, y, z}); }; F0R(i, N) { { int j = i + H.at(i); if (0 <= j && j < N) { try_triple(i, j, j + H.at(j)); try_triple(i, j, i + H.at(j)); try_triple(i, j - H.at(j), j); try_triple(i, i + H.at(j), j); } } { int j = i - H.at(i); if (0 <= j && j < N) { try_triple(j, i - H.at(j), i); } } } V<vi> with_sum(2 * N), with_diff(2 * N); auto get_diff = [&](int i) { return i - H.at(i) + N; }; auto get_sum = [&](int i) { return i + H.at(i); }; // {x, H[x]} {y, H[y]}, {z, H[z]} // H[x] + H[z] == z - x for (int i = 0; i < N; ++i) { with_diff.at(get_diff(i)).pb(get_sum(i)); with_sum.at(get_sum(i)).pb(get_diff(i)); } V<vi> edge_sum(2 * N), edge_diff(2 * N); for (int i = 0; i < N; ++i) { // dbg("sum", get_sum(i), "diff", get_diff(i)); if (sz(with_diff.at(get_diff(i))) < sz(with_sum.at(get_sum(i)))) { edge_sum.at(get_sum(i)).pb(get_diff(i)); } else { edge_diff.at(get_diff(i)).pb(get_sum(i)); } } vi ctr(2 * N); int ans = 0; F0R(s, 2 * N) { for (int d : with_sum.at(s)) ++ctr.at(d); // i - H[i] + N along the diag-down line // dbg(s, with_sum.at(s)); for (int d : edge_sum.at(s)) { // dbg("??", s, d, with_sum.at(s), with_diff.at(d)); for (int s2 : with_diff.at(d)) if (s2 < N) { // dbg("CHECKING", s, d - N, s2); ans += ctr.at(s2 + N); } } for (int d : with_sum.at(s)) --ctr.at(d); } F0R(d, 2 * N) { for (int s : with_diff.at(d)) ++ctr.at(s); for (int s : edge_diff.at(d)) { // dbg("??2", s, d, with_sum.at(s), with_diff.at(d)); for (int d2 : with_sum.at(s)) if (d2 >= N) { ans += ctr.at(d2 - N); } } for (int s : with_diff.at(d)) --ctr.at(s); } // F0R(a, N) FOR(b, a + 1, N) FOR(c, b + 1, N) { // if (H[a] + H[c] == H[b] && H[a] == c - b && H[c] == b - a) // ++ans; // } // (1, 1), (3, 3), (4, 2) // each(c, cands) dbg(c); // dbg(ans, sz(cands)); // dbg(ans, sz(cands)); return ans + sz(cands); } std::vector<int> construct_range(int M, int K) { // x = i - H[i] // y = i + H[i] // x < y // x + y achieves even numbers in range 0 ... 2 * N - 2 // want to minimize distinct {x, y}'s int N = M; vb seen(2 * N); vi contrib(N + 2); vb in_S(N + 2); vi S; auto valid_sum = [&](int s) { return s % 2 == 0 && s >= 0 && s < 2 * N; }; auto add_contrib = [&](int num, int inc) { if (0 <= num && num < sz(contrib)) contrib.at(num) += inc; }; auto add_sum = [&](int s) { assert(valid_sum(s)); assert(!seen.at(s)); seen.at(s) = true; for (int y : S) { int x = s - y; add_contrib(x, -1); } }; vpi pairs; auto add_S = [&](int x, int expected) { if (x >= 0) assert(!in_S.at(x)); vi sums_to_add; for (int y : S) { int s = x + y; if (valid_sum(s) && !seen.at(s)) { pairs.pb({min(x, y), max(x, y)}); sums_to_add.push_back(s); } } assert(sz(sums_to_add) == expected); for (int s = 0; s < 2 * N; s += 2) if (!seen.at(s)) { add_contrib(s - x, 1); } if (x >= 0) in_S.at(x) = true; S.pb(x); for (int s : sums_to_add) add_sum(s); }; add_S(-1, 0); for (int x : {1, 3, N + 1, N - 3}) { add_S(x, contrib.at(x)); } // -1 to N + 1 while (true) { // dbg("CHECKING", S); pi best{0, 1}; F0R(i, N + 2) if (!in_S.at(i)) ckmax(best, mp(contrib.at(i), -i)); if (best.f == 0) break; best.s *= -1; add_S(best.s, contrib.at(best.s)); } // dbg(pairs); vi H(N); assert(sz(pairs) == N); for (auto [x, y] : pairs) { assert(x < y); // dbg("HI", x, y); assert((x + y) % 2 == 0); H.at((y + x) / 2) = (y - x) / 2; } each(t, H) assert(1 <= t && t < N); // dbg(count_triples(H)); if (N == 20) { F0R(it, 10000) { // dbg(it, count_triples(H)); int i = rng() % N; pair<int, vi> best{0, {}}; vi cands; FOR(v, 1, N) cands.pb(v); shuffle(all(cands), rng); for (int v : cands) { H.at(i) = v; int x = count_triples(H); if (x > best.f) best = {x, H}; } H = best.s; } } return H; } // given S // y not in S // {[x in S] + y} < 2N // [[x in S] + y] not in S

Compilation message (stderr)

triples.cpp: In function 'void FileIO::setIn(str)':
triples.cpp:257:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  257 | void setIn(str s) { freopen(s.c_str(), "r", stdin); }
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
triples.cpp: In function 'void FileIO::setOut(str)':
triples.cpp:258:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  258 | 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...