이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad.h"
#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
#define int ll
using namespace std;
typedef long long ll;
const int N = 4e5 + 10;
int par[N];
int Find(int u){
if(par[u] == u) return u;
return par[u] = Find(par[u]);
}
bool Unite(int u, int v){
//cerr << "# " << u << ' ' << v << '\n';
u = Find(u); v = Find(v);
if(u == v) return false;
par[u] = v;
return true;
}
int deg[N];
ll plan_roller_coaster(vector<int32_t> s, vector<int32_t> t){
int n = s.size();
iota(par, par + N, 0);
vector<int> V;
for(auto x : s) V.pb(x);
for(auto x : t) V.pb(x);
V.pb(0);
V.pb(1e9 + 1);
sort(all(V));
V.resize(unique(all(V)) - V.begin());
int m = V.size();
for(auto &x : s) x = lower_bound(all(V), x) - V.begin();
for(auto &x : t) x = lower_bound(all(V), x) - V.begin();
for(auto x : s) deg[x] ++;
for(auto x : t) deg[x] --;
for(int i = 0; i < n; i++) Unite(s[i], t[i]);
//for(int i = 0; i < n; i++) cerr << s[i] << ' ' << t[i] << '\n';
vector< pair<int, int> > E;
deg[0] --;
deg[m - 1]++;
ll ans = 0;
for(int i = 0; i + 1 < m; i++){
int cnt = -deg[i];
if(cnt == 0) continue;
Unite(i, i + 1);
//cerr << i << ' ' << i + 1 << '\n';
deg[i + 1] += deg[i];
if(cnt < 0)
ans += abs(cnt) * (V[i + 1] - V[i]);
}
for(int i = 0; i + 1 < m; i++){
E.pb({V[i + 1] - V[i], i});
}
assert(deg[m - 1] == 0);
sort(all(E));
for(auto [w, i] : E){
if(Unite(i, i + 1)){
ans += w;
//cerr << "$$ " << w << '\n';
}
}
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... |