제출 #536142

#제출 시각아이디문제언어결과실행 시간메모리
536142KoDRoller Coaster Railroad (IOI16_railroad)C++17
100 / 100
271 ms18388 KiB
#include <bits/stdc++.h> #include "railroad.h" using ll = long long; using std::vector; using std::array; using std::pair; using std::tuple; int dedup(vector<int>& v) { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); return v.size(); } int lowb(const vector<int>& v, const int x) { return std::lower_bound(v.begin(), v.end(), x) - v.begin(); } struct union_find { vector<int> par; explicit union_find(const int n) : par(n, -1) {} int find(const int u) { return par[u] < 0 ? u : par[u] = find(par[u]); } bool merge(int x, int y) { x = find(x), y = find(y); if (x == y) return false; if (par[x] > par[y]) std::swap(x, y); par[x] += par[y]; par[y] = x; return true; } }; ll plan_roller_coaster(vector<int> s, vector<int> t) { const int n = s.size(); vector<int> cx; cx.reserve(2 * n + 2); cx.push_back(0); cx.push_back(1000000001); std::copy(s.begin(), s.end(), std::back_inserter(cx)); std::copy(t.begin(), t.end(), std::back_inserter(cx)); const int m = dedup(cx); vector<int> coeff(m); coeff[0] -= 1; coeff[m - 1] += 1; union_find dsu(m); for (int i = 0; i < n; ++i) { const int a = lowb(cx, s[i]); const int b = lowb(cx, t[i]); coeff[a] += 1; coeff[b] -= 1; dsu.merge(a, b); } ll cur = 0, ret = 0; vector<tuple<int, int, int>> edge; for (int i = 0; i < m; ++i) { if (i > 0) { if (cur != 0) dsu.merge(i - 1, i); if (cur > 0) ret += cur * (cx[i] - cx[i - 1]); edge.emplace_back(cx[i] - cx[i - 1], i - 1, i); } cur += coeff[i]; } std::sort(edge.begin(), edge.end()); for (const auto& [c, u, v] : edge) { if (dsu.merge(u, v)) { ret += c; } } return ret; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...