이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "railroad.h"
using namespace std;
struct data {
int x, y, z;
data () {}
data (int _x, int _y, int _z) {
x = _x, y = _y, z = _z;
}
bool operator < (const data &d) const {
return x < d.x;
}
};
const int INF = 2e9 + 10;
vector <int> p;
int find (int x) {
return p[x] == x ? x : p[x] = find(p[x]);
}
bool merge (int x, int y) {
x = find(x), y = find(y);
return p[x] = y, x != y;
}
long long plan_roller_coaster (vector <int> s, vector <int> t) {
s.push_back(INF);
t.push_back(1);
int n = (int) s.size();
for (int i = 0; i < n; ++i) {
p.push_back(i);
}
vector <data> good, edge;
for (int i = 0; i < n; ++i) {
good.emplace_back(s[i], i, 1);
good.emplace_back(t[i], i, -1);
}
sort(good.begin(), good.end());
long long ans = 0;
for (int i = 0, diff = 0; i + 1 < n + n; ++i) {
diff += good[i].z;
ans += max(0, diff) * 1LL * (good[i + 1].x - good[i].x);
edge.emplace_back(good[i + 1].x - good[i].x, good[i].y, good[i + 1].y);
if ((diff != 0) or (good[i].x == good[i + 1].x)) {
merge(good[i].y, good[i + 1].y);
}
}
sort(edge.begin(), edge.end());
for (int i = 0; i + 1 < n + n; ++i) {
if (merge(edge[i].y, edge[i].z)) {
ans += 1LL * edge[i].x;
}
}
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... |