Submission #989518

#TimeUsernameProblemLanguageResultExecution timeMemory
989518AdamGSRoller Coaster Railroad (IOI16_railroad)C++17
100 / 100
505 ms49024 KiB
#include "railroad.h" #include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const int LIM=4e5+7, INF=1e9+7; vector<int>skal; map<int,int>mp; ll jaki[LIM]; int F[LIM], akt; int fnd(int x) { if(F[x]==x) return x; return F[x]=fnd(F[x]); } bool uni(int a, int b) { if(fnd(a)==fnd(b)) return false; F[fnd(b)]=fnd(a); return true; } ll plan_roller_coaster(vector<int>s, vector<int>t) { s.pb(INF); t.pb(0); int n=s.size(); for(auto i : s) skal.pb(i); for(auto i : t) skal.pb(i); sort(all(skal)); for(auto i : skal) if(mp.find(i)==mp.end()) { jaki[akt]=i; mp[i]=akt++; } rep(i, akt) F[i]=i; vector<pair<int,int>>P; rep(i, n) { P.pb({s[i], -1}); P.pb({t[i], 1}); } sort(all(P)); ll lst=P[0].st, sum=0, ans=0; for(auto i : P) { if(i.st!=lst) { if(sum<0) ans-=sum*(i.st-lst); if(sum!=0) uni(mp[i.st], mp[lst]); } sum+=i.nd; lst=i.st; } rep(i, n) uni(mp[s[i]], mp[t[i]]); vector<pair<ll,pair<int,int>>>kraw; rep(i, akt-1) kraw.pb({jaki[i+1]-jaki[i], {i, i+1}}); sort(all(kraw)); for(auto i : kraw) if(uni(i.nd.st, i.nd.nd)) ans+=i.st; 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...