# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
906809 | mathnetic | Roller Coaster Railroad (IOI16_railroad) | C++17 | 543 ms | 29584 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "railroad.h"
using namespace std;
typedef pair<int, int> P;
typedef long long L;
unordered_map<int, int> m;
int f(int x) {
if (m[x] == 0) m[x] = x;
return (x == m[x]) ? x : (m[x] = f(m[x]));
}
void u(int x, int y) {
m[f(x)] = f(y);
}
bool c(P a, P b) {
return (a.second - a.first) < (b.second - b.first);
}
L plan_roller_coaster(vector<int> s, vector<int> t) {
L r = 0;
s.push_back(1e9);
t.push_back(1);
int n = s.size();
vector<P> v, d;
for (int i = 0; i < n; ++i) {
u(s[i], t[i]);
v.emplace_back(s[i], 1);
v.emplace_back(t[i], -1);
}
sort(v.begin(), v.end());
int b = 0;
for (int i = 0, j = 0; i < v.size(); i = j) {
for (; j < v.size() && v[j].first == v[i].first; ++j) {
b += v[j].second;
}
if (!(j >= v.size())) {
if (b > 0) {
r += static_cast<L>(b) * (v[j].first - v[i].first);
}
if (b == 0) {
d.emplace_back(v[i].first, v[j].first);
} else {
u(v[j].first, v[i].first);
}
}
}
sort(d.begin(), d.end(), c);
for (auto i : d) {
if (f(i.first) != f(i.second)) {
r += i.second - i.first;
u(i.first, i.second);
}
}
return r;
}
Compilation message (stderr)
# | 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... |