Submission #683167

#TimeUsernameProblemLanguageResultExecution timeMemory
683167cadmiumskyRoller Coaster Railroad (IOI16_railroad)C++17
100 / 100
584 ms56592 KiB
#include <bits/stdc++.h> #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long; //#define int ll #define sz(x) ((int)(x).size()) using pii = pair<int,int>; using tii = tuple<int,int,int>; const int nmax = 5e5 + 5, inf = 1e9 + 5; vector<tii> edg; namespace DSU { int dsu[nmax]; void init(int n) { for(int i = 0; i <= n; i++) dsu[i] = i; } int f(int x) { return x == dsu[x]? x: dsu[x] = f(dsu[x]); } int cost(int a, int b, int w) { a = f(a); b = f(b); if(a == b) return 0; dsu[a] = b; return w; } } long long plan_roller_coaster(vector<int> s, vector<int> t) { t.emplace_back(0); s.emplace_back(inf); unordered_map<int,int> nrm; map<int,int> smen; for(auto x : s) smen[x]++; for(auto x : t) smen[x]--; int edgc = 0; int last = 0, cnt = 0; ll cost = 0; for(auto [a, b] : smen) { nrm[a] = cnt++; if(edgc == 0) { //cerr << edgc << ' ' << a << ' ' << last << ' ' << cost << '+' << (a - last) * max(edgc, 0) << '\n'; edg.emplace_back(nrm[last], nrm[a], a - last); } else { //cerr << edgc << ' ' << a << ' ' << last << ' ' << cost << '+' << (a - last) * max(edgc, 0) << '\t'; cost += (ll)(a - last) * max(edgc, 0); //cerr << cost << '\n'; edg.emplace_back(nrm[last], nrm[a], 0); } edgc += b; last = a; } for(int i = 0; i < sz(s); i++) edg.emplace_back(nrm[s[i]], nrm[t[i]], 0); DSU::init(sz(nrm)); sort(all(edg), [](auto a, auto b) { return get<2>(a) < get<2>(b); }); for(auto [x, y, w] : edg) { //cerr << x << ' ' << y << ' ' << w << '\n'; cost += DSU::cost(x, y, w); } return cost; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...