# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
205968 | HungAnhGoldIBO2020 | Roller Coaster Railroad (IOI16_railroad) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=4e5+2;
const int inf=1e9+7;
vector<int> coor;
int head[N],cnt[N];
priority_queue<pair<int,pair<int,int> > > lis;
int findd(int x){
if(head[x]<0){
return x;
}
int y=findd(head[x]);
head[x]=y;
return y;
}
void unionn(int x,int y){
x=findd(x),y=findd(y);
head[x]+=head[y];
head[y]=x;
}
bool samee(int x,int y){
return findd(x)==findd(y);
}
int plan_roller_coaster(vector<int> s,vector<int> t){
s.push_back(inf);
t.push_back(1);
int n=s.size(),i,j,k,l,r,ans=0;
for(i=0;i<n;i++){
coor.push_back(s[i]);
coor.push_back(t[i]);
}
sort(coor.begin(),coor.end());
coor.erase(unique(coor.begin(),coor.end()),coor.end());
for(i=0;i<coor.size();i++){
head[i+1]=-1;
}
for(i=0;i<n;i++){
l=lower_bound(coor.begin(),coor.end(),s[i])-coor.begin()+1;
r=lower_bound(coor.begin(),coor.end(),t[i])-coor.begin()+1;
cnt[l]++;
cnt[r]--;
if(!samee(l,r)){
unionn(l,r);
}
}
for(i=1;i<=coor.size();i++){
cnt[i]+=cnt[i-1];
if(i==coor.size()){
assert(cnt[i]==0);
}
if(cnt[i]!=0){
if(!samee(i,i+1)){
unionn(i,i+1);
}
ans+=max(0ll,cnt[i])*(coor[i-1]-coor[i-2]);
}
}
for(i=1;i<coor.size();i++){
if(!samee(i,i+1)){
lis.push({-(coor[i]-coor[i-1]),{i,i+1}});
}
}
while(lis.size()){
i=lis.top().first;
j=lis.top().second.first;
k=lis.top().second.second;
lis.pop();
if(samee(j,k)){
continue;
}
unionn(j,k);
ans-=i;
}
return ans;
}
//signed main(){
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout<<plan_roller_coaster({1, 4, 5, 6}, {7, 3, 8, 6});
//}