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"railroad.h"
#include<iostream>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long ll;
int dsu[500000];
int get(int a){
if(dsu[a]<0)
return a;
return dsu[a]=get(dsu[a]);
}
void merge(int a,int b){
a=get(a);
b=get(b);
if(a==b)
return;
if(dsu[a]>dsu[b])
swap(a,b);
dsu[a]+=dsu[b];
dsu[b]=a;
}
ll plan_roller_coaster(vector<int>s,vector<int>t){
for(int i=0;i<500000;i++)
dsu[i]=-1;
s.push_back(1e9);
t.push_back(1);
int n=(int)s.size();
set<int>interestings;
for(int i:s)
interestings.insert(i);
for(int i:t)
interestings.insert(i);
vector<int>interesting(interestings.begin(),interestings.end());
map<int,int>m;
{
int idx=0;
for(int i:interesting)
m[i]=idx++;
}
vector<int>bal(interesting.size());
for(int&i:s)
i=m[i];
for(int&i:t)
i=m[i];
for(int i=0;i<n;i++){
int a=s[i],b=t[i];
merge(a,b);
bal[a]++;
bal[b]--;
}
ll res=0;
ll cbal=0;
vector<pair<int,int>>conns;
for(int i=0;i<(int)interesting.size()-1;i++){
ll dist=interesting[i+1]-interesting[i];
conns.emplace_back((int)dist,i);
cbal+=bal[i];
res+=max(0LL,cbal*dist);
if(cbal!=0)
merge(i,i+1);
}
sort(conns.begin(),conns.end());
for(auto conn:conns){
if(get(conn.second)!=get(conn.second+1)){
merge(conn.second,conn.second+1);
res+=conn.first;
}
}
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |