Submission #872814

#TimeUsernameProblemLanguageResultExecution timeMemory
872814islingrRoller Coaster Railroad (IOI16_railroad)C++17
100 / 100
154 ms20364 KiB
#include "bits/stdc++.h" using namespace std; #define rep(i, a, b) for (auto i{a}; i < (b); ++i) #define per(i, a, b) for (auto i{b}; i-- > (a); ) #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template <class T> bool uin(T& a, const T& b) { return a > b ? a = b, true : false; } template <class T> bool uax(T& a, const T& b) { return a < b ? a = b, true : false; } using ll = long long; #include <algorithm> #include <cassert> #include <vector> namespace atcoder { struct dsu { public: dsu() : _n(0) {} explicit dsu(int n) : _n(n), parent_or_size(n, -1) {} int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector<std::vector<int>> groups() { std::vector<int> leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector<std::vector<int>> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector<int>& v) { return v.empty(); }), result.end()); return result; } private: int _n; std::vector<int> parent_or_size; }; } // namespace atcoder ll plan_roller_coaster(vector<int> s, vector<int> t) { constexpr int inf = int(1e9) + 30; s.push_back(inf); t.push_back(1); auto cmp = t; cmp.insert(cmp.end(), all(s)); sort(all(cmp)); cmp.erase(unique(all(cmp)), end(cmp)); auto hsh = [&](int x) -> int { return lower_bound(all(cmp), x) - begin(cmp); }; const int m = sz(cmp); const int n = sz(s); atcoder::dsu g(m); vector<ll> bal(m); rep(i, 0, n) { const auto L = hsh(s[i]); const auto R = hsh(t[i]); ++bal[L]; --bal[R]; g.merge(L, R); } rep(i, 0, m - 1) bal[i + 1] += bal[i]; assert(bal.back() == 0); vector<pair<ll, int>> mst; ll res = 0; rep(i, 0, m - 1) { const auto len = cmp[i + 1] - cmp[i]; res += max(0ll, bal[i] * len); if (bal[i] == 0) mst.emplace_back(len, i); else g.merge(i, i + 1); } sort(all(mst)); for (auto [len, i] : mst) if (not g.same(i, i + 1)) { g.merge(i, i + 1); res += len; } return res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...