Submission #1031046

#TimeUsernameProblemLanguageResultExecution timeMemory
1031046WarinchaiRoller Coaster Railroad (IOI16_railroad)C++14
30 / 100
177 ms17224 KiB
#include "railroad.h" #include<bits/stdc++.h> using namespace std; int p[2000005],a[2000005]; int fp(int a){ return p[a]==a?a:p[a]=fp(p[a]); } void un(int a,int b){ p[fp(a)]=fp(b); } vector<int>ids; map<int,int>mp; long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) { int n = (int) s.size(); vector<int> ids; for(int i=0;i<n;i++){ ids.push_back(s[i]); ids.push_back(t[i]); } ids.push_back(INT_MIN); sort(ids.begin(),ids.end()); ids.erase(unique(ids.begin(),ids.end()),ids.end()); int m=ids.size(); for(int i=1;i<=m;i++)p[i]=i; for(int i=0;i<n;i++){ s[i]=lower_bound(ids.begin(),ids.end(),s[i])-ids.begin(); t[i]=lower_bound(ids.begin(),ids.end(),t[i])-ids.begin(); un(s[i],t[i]); a[s[i]]++,a[t[i]]--; } int val=0; int ans=0; for(int i=1;i<m;i++){ val+=a[i]; if(val>1)ans+=(val-1)*(ids[i+1]-ids[i]); else if(val<1)un(i,i+1); } vector<pair<int,int>>edges; for(int i=1;i<m;i++){ edges.push_back({ids[i+1]-ids[i],i}); } sort(edges.begin(),edges.end()); for(auto x:edges){ if(fp(x.second)!=fp(x.second+1))un(x.second,x.second+1),ans+=(ids[x.second+1]-ids[x.second]); } 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...