이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn=600000+10;
vector<int>allind;
int n,vorod[maxn],khoroj[maxn],inf=1e9+5;
struct dsu{
int par[maxn];
dsu(){
for(int i=0;i<maxn;i++){
par[i]=i;
}
}
int root(int u){
if(u==par[u]){
return u;
}
return par[u]=root(par[u]);
}
int con(int u,int v){
u=root(u);v=root(v);
if(u==v){
return 0;
}
par[u]=v;
return 1;
}
}ds,ds2;
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
s.push_back(inf);
t.push_back(1);
n=(int)s.size();
for(int i=0;i<n;i++){
allind.push_back(s[i]);
allind.push_back(t[i]);
}
sort(allind.begin(),allind.end());
allind.resize(unique(allind.begin(),allind.end())-allind.begin());
for(int i=0;i<n;i++){
int pp=lower_bound(allind.begin(),allind.end(),s[i])-allind.begin();
khoroj[pp]++;
int p=lower_bound(allind.begin(),allind.end(),t[i])-allind.begin();
vorod[p]++;
ds.con(p,pp);
}
long long mainres=0,vo=0,ko=0;
for(int i=0;i<(int)allind.size();i++){
ko+=khoroj[i];
vo+=vorod[i];
if(ko>vo){
mainres+=ko-vo;
}
if(ko!=vo){
ds.con(i,i+1);
}
}
vector<pair<long long ,pair<long long ,long long>>>allv;
for(int i=0;i<(int)allind.size()-1;i++){
if(ds.root(i)!=ds.root(i+1)){
allv.push_back(make_pair(allind[i+1]-allind[i],make_pair(ds.root(i),ds.root(i+1))));
}
}
sort(allv.begin(),allv.end());
for(int i=0;i<(int)allv.size();i++){
if(ds2.con(allv[i].second.first,allv[i].second.second)){
mainres+=allv[i].first;
}
}
return mainres;
}
# | 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... |