이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define vll vector<ll>
#define fr first
#define bc back()
#define sc second
const ll inf=1e9+10;
const ll N=5e5+7;
vector<vll> g(N);
ll p[N],siz[N];
ll anc(ll a){
return (a==p[a] ? a : p[a]=anc(p[a]));
}
bool uni(ll a,ll b){
a=anc(a),b=anc(b);
if(a==b)return false;
if(siz[a]>siz[b])swap(a,b);
siz[b]+=siz[a];
p[a]=b;
return true;
}
long long plan_roller_coaster(vector<int> s, vector<int> t) {
ll i;
ll n=s.size();
//compress;
set<int> st;
map<int,ll> pos,mp;
for(i=0;i<n;i++){
st.insert(s[i]);
st.insert(t[i]);
}
st.insert(inf);
ll c=0;
for(auto i : st){
pos[i]=c;
mp[c]=i;
c++;
}
vll val(c);
for(i=0;i<c;i++){
val[i]=mp[i];
p[i]=i;
siz[i]=1;
}
vll vs,vt;
vll pref(c);
pref[c-1]++;
pref[0]--;
for(i=0;i<n;i++){
vs.pb(pos[s[i]]);
vt.pb(pos[t[i]]);
g[vs[i]].pb(vt[i]);
pref[vs[i]]++;
pref[vt[i]]--;
}
ll ans=0;
for(i=0;i<c-1;i++){
if(i>0)pref[i]+=pref[i-1];
ll C=pref[i];
while(C>0){
ans+=(val[i+1]-val[i]);
if(C==pref[i])g[i+1].pb(i);
C--;
}
while(C<0){
if(c==pref[i])g[i].pb(i+1);
C++;
}
}
uni(0,c-1);
for(i=0;i<c;i++){
for(auto j : g[i]){
uni(i,j);
}
}
vector<pair<ll,ll>> edges;
for(i=1;i<c;i++){
edges.pb({val[i]-val[i-1],i});
}
sort(edges.begin(),edges.end());
for(i=0;i<(ll)edges.size();i++){
if(uni(edges[i].sc,edges[i].sc-1)){
ans+=edges[i].fr;
}
}
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... |