#include "railroad.h"
#include <algorithm>
using namespace std;
typedef long long llong;
typedef pair<int, int> pi;
int n;
vector<int> comp;
int bsearch(int x) {
int s = 0, e = comp.size() - 1, m;
while (s < e) {
m = (s + e) / 2;
if (comp[m] == x) return m;
if (comp[m] < x) s = m + 1;
else e = m - 1;
}
return s;
}
int degree[400000];
int group[400000];
int par[400000];
vector<int> edge[400000];
pi mst[400000];
void dfs(int x, int s) {
group[x] = s;
for (int i : edge[x]) {
if (group[i] > 0) continue;
dfs(i, s);
}
}
int find(int x) {
if (par[x]) return par[x] = find(par[x]);
return x;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
n = s.size();
comp.resize(n << 1);
for (int i = 0; i < n; ++i) {
comp[i << 1] = s[i];
comp[i << 1 | 1] = t[i];
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i = 0; i < n; ++i) {
s[i] = bsearch(s[i]);
t[i] = bsearch(t[i]);
++degree[s[i]];
--degree[t[i]];
edge[s[i]].push_back(t[i]);
}
int need;
llong ans = 0ll;
for (int i = 0; i + 1 < comp.size(); ++i) {
need = (i == 0) - degree[i];
degree[i + 1] -= need;
degree[i] += need;
if (need > 0) {
edge[i].push_back(i + 1);
}
else if (need < 0) {
edge[i + 1].push_back(i);
ans -= 1ll * need * (comp[i + 1] - comp[i]);
}
}
int j = 0;
for (int i = 0; i < comp.size(); ++i) {
if (group[i]) continue;
dfs(i, ++j);
}
for (int i = 0, j = 0; i + 1 < comp.size(); ++i) {
if (group[i] != group[i + 1]) {
mst[j++] = { comp[i + 1] - comp[i], i };
}
}
sort(mst, mst + j);
for (int i = 0; i < j; ++i) {
int x = find(group[mst[i].second]), y = find(group[mst[i].second + 1]);
if (x == y) continue;
ans += mst[i].first;
par[x] = y;
}
return ans;
}
Compilation message
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:58:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i + 1 < comp.size(); ++i) {
^
railroad.cpp:71:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < comp.size(); ++i) {
^
railroad.cpp:75:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0, j = 0; i + 1 < comp.size(); ++i) {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
19120 KB |
n = 2 |
2 |
Correct |
3 ms |
19120 KB |
n = 2 |
3 |
Correct |
6 ms |
19120 KB |
n = 2 |
4 |
Correct |
0 ms |
19120 KB |
n = 2 |
5 |
Correct |
6 ms |
19120 KB |
n = 2 |
6 |
Correct |
3 ms |
19120 KB |
n = 2 |
7 |
Correct |
0 ms |
19120 KB |
n = 3 |
8 |
Correct |
6 ms |
19120 KB |
n = 3 |
9 |
Correct |
3 ms |
19120 KB |
n = 3 |
10 |
Correct |
3 ms |
19120 KB |
n = 8 |
11 |
Correct |
0 ms |
19120 KB |
n = 8 |
12 |
Correct |
0 ms |
19120 KB |
n = 8 |
13 |
Correct |
3 ms |
19120 KB |
n = 8 |
14 |
Correct |
0 ms |
19120 KB |
n = 8 |
15 |
Correct |
3 ms |
19120 KB |
n = 8 |
16 |
Correct |
0 ms |
19120 KB |
n = 8 |
17 |
Correct |
3 ms |
19120 KB |
n = 8 |
18 |
Correct |
3 ms |
19120 KB |
n = 8 |
19 |
Correct |
3 ms |
19120 KB |
n = 3 |
20 |
Correct |
0 ms |
19120 KB |
n = 7 |
21 |
Correct |
3 ms |
19120 KB |
n = 8 |
22 |
Incorrect |
3 ms |
19120 KB |
answer is not correct: 50543464 instead of 46882726 |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
19120 KB |
n = 2 |
2 |
Correct |
3 ms |
19120 KB |
n = 2 |
3 |
Correct |
6 ms |
19120 KB |
n = 2 |
4 |
Correct |
0 ms |
19120 KB |
n = 2 |
5 |
Correct |
6 ms |
19120 KB |
n = 2 |
6 |
Correct |
3 ms |
19120 KB |
n = 2 |
7 |
Correct |
0 ms |
19120 KB |
n = 3 |
8 |
Correct |
6 ms |
19120 KB |
n = 3 |
9 |
Correct |
3 ms |
19120 KB |
n = 3 |
10 |
Correct |
3 ms |
19120 KB |
n = 8 |
11 |
Correct |
0 ms |
19120 KB |
n = 8 |
12 |
Correct |
0 ms |
19120 KB |
n = 8 |
13 |
Correct |
3 ms |
19120 KB |
n = 8 |
14 |
Correct |
0 ms |
19120 KB |
n = 8 |
15 |
Correct |
3 ms |
19120 KB |
n = 8 |
16 |
Correct |
0 ms |
19120 KB |
n = 8 |
17 |
Correct |
3 ms |
19120 KB |
n = 8 |
18 |
Correct |
3 ms |
19120 KB |
n = 8 |
19 |
Correct |
3 ms |
19120 KB |
n = 3 |
20 |
Correct |
0 ms |
19120 KB |
n = 7 |
21 |
Correct |
3 ms |
19120 KB |
n = 8 |
22 |
Incorrect |
3 ms |
19120 KB |
answer is not correct: 50543464 instead of 46882726 |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
359 ms |
48728 KB |
n = 199999 |
2 |
Correct |
416 ms |
41600 KB |
n = 199991 |
3 |
Correct |
369 ms |
48264 KB |
n = 199993 |
4 |
Correct |
273 ms |
35876 KB |
n = 152076 |
5 |
Correct |
156 ms |
29460 KB |
n = 93249 |
6 |
Correct |
353 ms |
37232 KB |
n = 199910 |
7 |
Correct |
386 ms |
41932 KB |
n = 199999 |
8 |
Correct |
336 ms |
39060 KB |
n = 199997 |
9 |
Correct |
369 ms |
38268 KB |
n = 171294 |
10 |
Correct |
266 ms |
34608 KB |
n = 140872 |
11 |
Correct |
319 ms |
37392 KB |
n = 199886 |
12 |
Correct |
346 ms |
41760 KB |
n = 199996 |
13 |
Correct |
339 ms |
39716 KB |
n = 200000 |
14 |
Correct |
329 ms |
44516 KB |
n = 199998 |
15 |
Correct |
336 ms |
42420 KB |
n = 200000 |
16 |
Correct |
346 ms |
42520 KB |
n = 199998 |
17 |
Correct |
346 ms |
42480 KB |
n = 200000 |
18 |
Correct |
373 ms |
41712 KB |
n = 190000 |
19 |
Correct |
316 ms |
45364 KB |
n = 177777 |
20 |
Correct |
189 ms |
30148 KB |
n = 100000 |
21 |
Correct |
413 ms |
41572 KB |
n = 200000 |
22 |
Correct |
369 ms |
36360 KB |
n = 200000 |
23 |
Correct |
399 ms |
48732 KB |
n = 200000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
19120 KB |
n = 2 |
2 |
Correct |
3 ms |
19120 KB |
n = 2 |
3 |
Correct |
6 ms |
19120 KB |
n = 2 |
4 |
Correct |
0 ms |
19120 KB |
n = 2 |
5 |
Correct |
6 ms |
19120 KB |
n = 2 |
6 |
Correct |
3 ms |
19120 KB |
n = 2 |
7 |
Correct |
0 ms |
19120 KB |
n = 3 |
8 |
Correct |
6 ms |
19120 KB |
n = 3 |
9 |
Correct |
3 ms |
19120 KB |
n = 3 |
10 |
Correct |
3 ms |
19120 KB |
n = 8 |
11 |
Correct |
0 ms |
19120 KB |
n = 8 |
12 |
Correct |
0 ms |
19120 KB |
n = 8 |
13 |
Correct |
3 ms |
19120 KB |
n = 8 |
14 |
Correct |
0 ms |
19120 KB |
n = 8 |
15 |
Correct |
3 ms |
19120 KB |
n = 8 |
16 |
Correct |
0 ms |
19120 KB |
n = 8 |
17 |
Correct |
3 ms |
19120 KB |
n = 8 |
18 |
Correct |
3 ms |
19120 KB |
n = 8 |
19 |
Correct |
3 ms |
19120 KB |
n = 3 |
20 |
Correct |
0 ms |
19120 KB |
n = 7 |
21 |
Correct |
3 ms |
19120 KB |
n = 8 |
22 |
Incorrect |
3 ms |
19120 KB |
answer is not correct: 50543464 instead of 46882726 |
23 |
Halted |
0 ms |
0 KB |
- |