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 <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
int n, X[410000], cnt, Ideg[410000], Odeg[410000], P[410000];
bool v[410000];
int UF[410000];
struct Edge{
int a, b, c;
bool operator<(const Edge &p)const{
return c<p.c;
}
}w[410000];
int Find(int a){
if(a==UF[a])return a;
return UF[a] = Find(UF[a]);
}
void Merge(int a, int b){
a=Find(a),b=Find(b);
if(a!=b)UF[a]=b;
}
void Add(int a, int b, int c){
if(c<=0)return;
Odeg[a]+=c, Ideg[b]+=c;
Merge(a,b);
}
long long plan_roller_coaster(vector<int> s, vector<int> t) {
n = s.size();
int i;
for(i=0;i<n;i++){
X[++cnt] = s[i];
X[++cnt] = t[i];
}
X[++cnt] = 0, X[++cnt] = 2e9;
sort(X+1,X+cnt+1);
for(i=1;i<=cnt;i++)UF[i] = i;
for(i=0;i<n;i++){
s[i] = lower_bound(X+1,X+cnt+1,s[i])-X;
t[i] = lower_bound(X+1,X+cnt+1,t[i])-X;
Add(s[i],t[i],1);
}
Add(cnt,1,1);
int sum = 0;
long long res = 0;
for(i=1;i<=cnt;i++){
sum += Ideg[i] - Odeg[i];
P[i] = -sum;
if(sum < 0){
res += 1ll * (-sum) * (X[i+1]-X[i]);
}
}
for(i=1;i<cnt;i++){
Add(i+1,i,P[i]);
}
for(i=1;i<cnt;i++){
Add(i,i+1,Ideg[i]-Odeg[i]);
w[i].a=i,w[i].b=i+1,w[i].c=X[i+1]-X[i];
}
sort(w+1,w+cnt);
for(i=1;i<cnt;i++){
if(Find(w[i].a)!=Find(w[i].b)){
res += w[i].c;
Merge(w[i].a,w[i].b);
}
}
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... |