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 <bits/stdc++.h>
using namespace std;
using ll = long long int;
using pll = pair<ll,ll>;
#define pb push_back
#define F first
#define S second
vector<ll> cp,ps,p;
vector<pll> vec;
int n;
ll ans;
ll gp(ll x){
return p[x]==-1?x:p[x]=gp(p[x]);
}
bool uni(ll v, ll u){
v=gp(v);
u=gp(u);
if(v==u) return false;
p[u]=v;
return true;
}
ll plan_roller_coaster(vector<int> s, vector<int> t) {
s.pb(1e9+10);
t.pb(1);
for(int i=0; i<(int)s.size(); i++){
cp.pb(s[i]);
cp.pb(t[i]);
}
sort(cp.begin(),cp.end());
cp.resize(unique(cp.begin(),cp.end())-cp.begin());
n=cp.size();
ps.resize(n);
p.resize(n,-1);
for(int i=0; i<(int)s.size(); i++){
s[i]=lower_bound(cp.begin(),cp.end(),s[i])-cp.begin();
t[i]=lower_bound(cp.begin(),cp.end(),t[i])-cp.begin();
uni(s[i],t[i]);
ps[s[i]]++;
ps[t[i]]--;
}
for(int i=1; i<n; i++) ps[i]+=ps[i-1];
for(int i=0; i<n-1; i++){
if(ps[i]){
uni(i,i+1);
if(ps[i]>0) ans+=ps[i]*(cp[i+1]-cp[i]);
}
vec.pb({cp[i+1]-cp[i],i});
}
sort(vec.begin(),vec.end());
for(auto [w,i] : vec) if(uni(i,i+1)) ans+=w;
return ans;
}
# | 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... |