이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad.h"
#include <bits/stdc++.h>
#define N 500005
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
struct edge{ll c;int u,v;};
int n,c,group[N],c_g,p[N],en,x;
ll ans,sum[N],from[N];
map<int,int> mp,to;
vector<int> g1[N];
edge g2[N];
int fi(int x)
{
if(p[x]==x)
return x;
return p[x]=fi(p[x]);
}
void un(int x,int y)
{
x=fi(x),y=fi(y);
if(x==y)
return;
p[y]=x;
}
void dfs(int curr)
{
group[curr]=c_g;
for(auto &it : g1[curr]){
if(!group[it])
dfs(it);
}
}
bool sortf(edge e1,edge e2){return e1.c<e2.c;}
ll plan_roller_coaster(vector<int> s,vector<int> t)
{
n=s.size();
for(int i=0;i<n;i++){
mp[s[i]]++;
mp[t[i]]++;
}
mp[1]++;
for(auto &it : mp){
to[it.first]=++c;
from[c]=it.first;
}
sum[2]--;
g1[c].push_back(1);
for(int i=0;i<n;i++){
if(s[i]==t[i])continue;
if(s[i]<t[i]){
sum[to[s[i]]+1]++;
sum[to[t[i]]+1]--;
}
else{
sum[to[s[i]]+1]++;
sum[to[t[i]]+1]--;
}
g1[to[s[i]]].push_back(to[t[i]]);
}
for(int i=2;i<=c;i++){
sum[i]+=sum[i-1];
if(sum[i]<0)
g1[i-1].push_back(i);
if(sum[i]>0){
g1[i].push_back(i-1);
ans+=sum[i]*(from[i]-from[i-1]);
}
}
for(int i=1;i<=c;i++){
if(group[i])
continue;
c_g++;
p[c_g]=c_g;
dfs(i);
}
for(int i=2;i<=c;i++){
if(group[i-1]!=group[i]){
g2[++en]={from[i]-from[i-1],group[i-1],group[i]};
}
}
sort(g2+1,g2+1+en,sortf);
for(int i=1;i<=en;i++){
int u=g2[i].u,v=g2[i].v;
if(fi(u)==fi(v))
continue;
ans+=g2[i].c;
un(u,v);
}
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... |