Submission #1021380

#TimeUsernameProblemLanguageResultExecution timeMemory
1021380mansurRoller Coaster Railroad (IOI16_railroad)C++17
100 / 100
151 ms18224 KiB
#include "railroad.h" #include <bits/stdc++.h> using namespace std; #define rall(s) s.rbegin(), s.rend() #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() #define s second #define f first using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int N = 1e6, inf = 1e9 + 5; int pr[N], sz[N]; int get(int x) { if (pr[x] == x) return x; return pr[x] = get(pr[x]); } void unite(int a, int b) { a = get(a); b = get(b); if (a == b) return; if (sz[a] > sz[b]) swap(a, b); pr[a] = b, sz[b] += sz[a]; } ll plan_roller_coaster(vector<int> s, vector<int> t) { vector<int> x; t.push_back(1); s.push_back(inf); for (int v: s) x.push_back(v); for (int v: t) x.push_back(v); sort(all(x)); x.erase(unique(all(x)), x.end()); int n = sz(s), m = sz(x); vector<int> cnt(m); for (int i = 0; i < m; i++) pr[i] = i, sz[i] = 1; for (int i = 0; i < n; i++) { s[i] = lower_bound(all(x), s[i]) - x.begin(); t[i] = lower_bound(all(x), t[i]) - x.begin(); unite(s[i], t[i]); if (s[i] <= t[i]) cnt[s[i]]++, cnt[t[i]]--; else cnt[t[i]]--, cnt[s[i]]++; } ll ans = 0; vector<array<int, 3>> g; for (int i = 0; i < m; i++) { if (i) cnt[i] += cnt[i - 1]; if (cnt[i]) unite(i, i + 1); else g.push_back({x[i + 1] - x[i], i, i + 1}); if (cnt[i] > 0) ans += (x[i + 1] - x[i]) * 1ll * cnt[i]; } sort(all(g)); for (auto [w, a, b]: g) { a = get(a), b = get(b); if (a == b) continue; ans += w; unite(a, b); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...