이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad.h"
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
using namespace std;
typedef long long ll;
int balance[404040], din[404040], dout[404040];
vector<pair<int, pair<int, int>>> edges;
int cmp[404040];
int find(int a) {
while (cmp[a] != a) cmp[a] = cmp[cmp[a]], a = cmp[a];
return a;
}
void onion(int a, int b) { cmp[find(a)] = cmp[find(b)]; }
ll plan_roller_coaster(vector<int> s, vector<int> t) {
int n = s.size();
FOR(i, 0, n) {
balance[i * 2] = s[i];
balance[i * 2 + 1] = t[i];
}
balance[2 * n] = 1;
balance[2 * n + 1] = 2e9;
sort(balance, balance + 2 * n + 2);
FOR(i, 0, 2 * n + 2) cmp[i] = i;
FOR(i, 0, n) {
s[i] = lower_bound(balance, balance + 2 * n + 2, s[i]) - balance;
t[i] = lower_bound(balance, balance + 2 * n + 2, t[i]) - balance;
din[t[i]]++;
dout[s[i]]++;
onion(t[i], s[i]);
}
din[0]++;
dout[2 * n + 1]++;
onion(0, 2 * n + 1);
ll ans = 0, sm = 0;
FOR(i, 0, 2 * n + 1) {
sm += dout[i] - din[i];
if (sm < 0)
onion(i, i + 1);
else if (sm == 0)
edges.push_back({balance[i + 1] - balance[i], {i, i + 1}});
else {
ans += sm * (balance[i + 1] - balance[i]);
onion(i, i + 1);
}
}
sort(edges.begin(), edges.end());
for (auto& i : edges) {
if (find(i.second.first) != find(i.second.second)) {
onion(i.second.first, i.second.second);
ans += i.first;
}
}
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... |