#include "railroad.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef long long lint;
struct disj{
int pa[400005];
void init(int n){
for(int i=0; i<=n; i++) pa[i] = i;
}
int find(int x){
return pa[x] = (pa[x] == x ? x : find(pa[x]));
}
bool uni(int p, int q){
p = find(p);
q = find(q);
if(p == q) return 0;
pa[q] = p; return 1;
}
}disj;
struct edg{
int s, e, x;
bool operator<(const edg &e)const{
return x < e.x;
}
};
int dx[400005];
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
vector<int> crd;
int n = (int) s.size();
for(int i=0; i<n; i++){
crd.push_back(s[i]);
crd.push_back(t[i]);
}
sort(crd.begin(), crd.end());
crd.resize(unique(crd.begin(), crd.end()) - crd.begin());
disj.init(crd.size());
for(int i=0; i<n; i++){
s[i] = lower_bound(crd.begin(), crd.end(), s[i]) - crd.begin();
t[i] = lower_bound(crd.begin(), crd.end(), t[i]) - crd.begin();
dx[s[i]]++;
dx[t[i]]--;
disj.uni(s[i], t[i]);
}
int cur = 1;
lint ans = 0;
vector<edg> v;
for(int i=crd.size() - 1; i>=0; i--){
cur += dx[i];
if(cur < 0){
ans += -1ll * cur * (crd[i] - crd[i-1]);
dx[i-1] += cur;
cur = 0;
}
if(cur > 0 && i-1 < crd.size()) disj.uni(i, i-1);
else if(cur == 0) v.push_back({i, i-1, crd[i] - crd[i-1]});
}
sort(v.begin(), v.end());
for(auto &i : v){
if(disj.uni(i.s, i.e)) ans += i.x;
}
return ans;
}
Compilation message
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:59:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(cur > 0 && i-1 < crd.size()) disj.uni(i, i-1);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
5152 KB |
n = 2 |
2 |
Correct |
0 ms |
5152 KB |
n = 2 |
3 |
Correct |
0 ms |
5152 KB |
n = 2 |
4 |
Correct |
0 ms |
5152 KB |
n = 2 |
5 |
Correct |
0 ms |
5152 KB |
n = 2 |
6 |
Incorrect |
0 ms |
5152 KB |
answer is not correct: 592737449 instead of 523688153 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
5152 KB |
n = 2 |
2 |
Correct |
0 ms |
5152 KB |
n = 2 |
3 |
Correct |
0 ms |
5152 KB |
n = 2 |
4 |
Correct |
0 ms |
5152 KB |
n = 2 |
5 |
Correct |
0 ms |
5152 KB |
n = 2 |
6 |
Incorrect |
0 ms |
5152 KB |
answer is not correct: 592737449 instead of 523688153 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
236 ms |
11444 KB |
n = 199999 |
2 |
Correct |
273 ms |
21176 KB |
n = 199991 |
3 |
Correct |
303 ms |
21176 KB |
n = 199993 |
4 |
Correct |
156 ms |
10692 KB |
n = 152076 |
5 |
Correct |
103 ms |
8244 KB |
n = 93249 |
6 |
Correct |
173 ms |
11444 KB |
n = 199910 |
7 |
Correct |
199 ms |
11444 KB |
n = 199999 |
8 |
Correct |
199 ms |
11444 KB |
n = 199997 |
9 |
Correct |
196 ms |
10996 KB |
n = 171294 |
10 |
Correct |
136 ms |
10516 KB |
n = 140872 |
11 |
Correct |
196 ms |
11444 KB |
n = 199886 |
12 |
Correct |
196 ms |
11444 KB |
n = 199996 |
13 |
Correct |
209 ms |
11444 KB |
n = 200000 |
14 |
Correct |
209 ms |
16568 KB |
n = 199998 |
15 |
Correct |
209 ms |
16568 KB |
n = 200000 |
16 |
Correct |
226 ms |
16568 KB |
n = 199998 |
17 |
Correct |
283 ms |
21176 KB |
n = 200000 |
18 |
Correct |
239 ms |
16408 KB |
n = 190000 |
19 |
Correct |
199 ms |
11092 KB |
n = 177777 |
20 |
Correct |
113 ms |
8340 KB |
n = 100000 |
21 |
Correct |
206 ms |
11444 KB |
n = 200000 |
22 |
Correct |
269 ms |
16568 KB |
n = 200000 |
23 |
Correct |
243 ms |
16568 KB |
n = 200000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
5152 KB |
n = 2 |
2 |
Correct |
0 ms |
5152 KB |
n = 2 |
3 |
Correct |
0 ms |
5152 KB |
n = 2 |
4 |
Correct |
0 ms |
5152 KB |
n = 2 |
5 |
Correct |
0 ms |
5152 KB |
n = 2 |
6 |
Incorrect |
0 ms |
5152 KB |
answer is not correct: 592737449 instead of 523688153 |
7 |
Halted |
0 ms |
0 KB |
- |