Submission #1021845

#TimeUsernameProblemLanguageResultExecution timeMemory
1021845ttamxRoller Coaster Railroad (IOI16_railroad)C++17
0 / 100
148 ms11444 KiB
#include "railroad.h" #include <bits/stdc++.h> using namespace std; using ll = long long; ll plan_roller_coaster(vector<int> s,vector<int> t){ int n=s.size(); vector<int> a; for(int i=0;i<n;i++){ a.emplace_back(s[i]); a.emplace_back(t[i]); } sort(a.begin(),a.end()); a.erase(unique(a.begin(),a.end()),a.end()); int m=a.size(); for(auto &x:s)x=lower_bound(a.begin(),a.end(),x)-a.begin(); for(auto &x:t)x=lower_bound(a.begin(),a.end(),x)-a.begin(); vector<int> b(m),p(m); iota(p.begin(),p.end(),0); function<int(int)> fp=[&](int u){ return p[u]=u==p[u]?u:fp(p[u]); }; auto uni=[&](int u,int v){ p[fp(u)]=fp(v); }; for(int i=0;i<n;i++){ b[s[i]]++,b[t[i]]--; uni(s[i],t[i]); } ll ans=0; for(int i=0;i<m-1;i++){ b[i+1]+=b[i]; ans+=1LL*max(0,b[i])*(a[i+1]-a[i]); if(a[i]!=1)uni(i,i+1); else if(fp(i)!=fp(i+1))uni(i,i+1),ans+=a[i+1]-a[i]; } 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...