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>
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define umax(x,y) x=max(x,y)
#define umin(x,y) x=min(x,y)
#define ll long long
#define ii pair<int,int>
#define iii pair<int,ii>
#define iiii pair<ii,ii>
#define sz(x) ((int) x.size())
#define orta ((bas+son)/2)
#define all(x) x.begin(),x.end()
#define inf 1000000000
#define MOD 1000000007
#define N 400005
#define LOG 20
#define KOK 300
#define EPS 0.0000001
using namespace std;
int n,cnt;
vector<int> pre,dad;
int tut[N];
struct edge {
int a,b,c;
};
int find(int x) {return dad[x]=(x==dad[x]?x:find(dad[x]));}
void merge(int x,int y) {
dad[find(x)]=find(y);
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
map<int,int> mp;
n=sz(s);
for(int i=0;i<n;i++) {
mp[s[i]]=1;
mp[t[i]]=1;
}
mp[inf]=1;
for(auto& x:mp) {
x.nd=++cnt;
tut[cnt]=x.st;
}
dad=pre=vector<int>(cnt+5,0);
for(int i=1;i<=cnt;i++) dad[i]=i;
for(int i=0;i<n;i++) {
// cerr<<s[i]<<" "<< t[i]<<"\n";
s[i]=mp[s[i]];
t[i]=mp[t[i]];
merge(s[i],t[i]);
// cerr<< "to : \n";
// cerr<<s[i] <<" "<<t[i]<<"\n";
// cerr<<"-------------------\n";
if(s[i]<=t[i]) {
pre[s[i]]++;
pre[t[i]]--;
}
else {
pre[t[i]]--;
pre[s[i]]++;
}
}
ll ans=0;
vector<edge> q;
for(int i=1;i<cnt;i++) {
pre[i]+=pre[i-1];
// cerr<<i << " " << pre[i] <<"\n";
if(pre[i]>1) {
ans+=pre[i]-1;
}
if(pre[i]!=1) merge(i,i+1);
if(find(i)!=find(i+1)) q.pb({i,i+1,tut[i+1]-tut[i]});
}
sort(all(q),[](edge x,edge y) {
return x.c<y.c;
});
for(auto x:q) {
if(find(x.a)!=find(x.b)) {
merge(x.a,x.b);
ans+=x.c;
}
}
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... |