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 ll long long
using namespace std;
struct dsu{
int par[400005];
void init(int N){
for(int i = 0;i<=N;++i) par[i] = i;
}
int finden(int x){
return par[x] = (par[x] == x ? x : finden(par[x]));
}
bool unite(int x, int y){
x = finden(x); y = finden(y);
if(x == y){
return false;
}
par[y] = x;
return true;
}
}D;
struct edge{
int start, ende, wert;
bool operator<(const edge &e)const{
return wert < e.wert;
}
};
int cnt[400005];
ll plan_roller_coaster(vector<int> s, vector<int> t){
vector<int> merged; int n = (int)s.size();
for(int i = 0;i<n;++i) {merged.push_back(s[i]); merged.push_back(t[i]);}
sort(merged.begin(),merged.end());
merged.resize(unique(merged.begin(),merged.end())-merged.begin());
D.init(merged.size());
for(int i = 0;i<n;++i){
s[i] = lower_bound(merged.begin(),merged.end(),s[i])-merged.begin();
t[i] = lower_bound(merged.begin(),merged.end(),t[i])-merged.begin();
cnt[s[i]]++; cnt[t[i]]--; D.unite(s[i],t[i]);
}
int cur = 1; ll ans = 0;
vector<edge> kanten;
for(int i = merged.size()-1;i>=0;--i){
cur += cnt[i];
if(cur < 0){
ans += -1ll * cur * (merged[i]-merged[i-1]);
cnt[i-1] += cur;
D.unite(i-1,i);
cur = 0;
}
if(cur > 0 && i) D.unite(i,i-1);
else if(cur == 0) kanten.push_back({i-1,i,merged[i]-merged[i-1]});
}
sort(kanten.begin(),kanten.end());
for(auto p : kanten){
if(D.unite(p.start,p.ende)) ans += p.wert;
}
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... |