#include<bits/stdc++.h>
using namespace std;
#include "railroad.h"
int pr[400001];
int find(int x){
if(x==pr[x])return x;
return pr[x] = find(pr[x]);
}
bool mergegroup(int a,int b){
a = find(a); b = find(b);
if(a==b)return 0;
pr[a] = b;
return 1;
}
long long plan_roller_coaster(vector<int> s, vector<int> t){
set<int> ss;
for(auto i:s)ss.insert(i);
for(auto i:t)ss.insert(i);
map<int,int> sav;
int rev[s.size()+t.size()+1];
int cnt = 0;
for(auto i:ss){
sav[i] = ++cnt;
rev[cnt] = i;
}
int in[cnt+1] = {0};
int out[cnt+1] = {0};
for(int i = 1;i<=cnt;i++)pr[i] = i;
for(int i = 0;i<s.size();i++){
in[sav[s[i]]]++;
out[sav[t[i]]]++;
mergegroup(sav[s[i]],sav[t[i]]);
}
in[cnt]++;
out[1]++;
mergegroup(1,cnt);
long long ans = 0;
vector<array<int,3>> ed;
for(int i = cnt;i>1;i--){
if(out[i]>in[i]){
ans+=(out[i]-in[i])*(rev[i]-rev[i-1]);
out[i-1]+=(out[i]-in[i]);
mergegroup(i,i-1);
}else if(in[i]>out[i]){
in[i-1]+=(in[i]-out[i]);
mergegroup(i,i-1);
}
ed.push_back({rev[i]-rev[i-1],i,i-1});
}
sort(ed.begin(),ed.end());
for(auto i:ed){
if(mergegroup(i[1],i[2]))ans+=i[0];
}
return ans;
}
Compilation message
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:30:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i = 0;i<s.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n = 2 |
2 |
Correct |
1 ms |
348 KB |
n = 2 |
3 |
Correct |
1 ms |
348 KB |
n = 2 |
4 |
Correct |
1 ms |
348 KB |
n = 2 |
5 |
Correct |
1 ms |
348 KB |
n = 2 |
6 |
Correct |
0 ms |
348 KB |
n = 2 |
7 |
Correct |
0 ms |
348 KB |
n = 3 |
8 |
Correct |
0 ms |
348 KB |
n = 3 |
9 |
Correct |
0 ms |
344 KB |
n = 3 |
10 |
Correct |
0 ms |
348 KB |
n = 8 |
11 |
Correct |
0 ms |
348 KB |
n = 8 |
12 |
Correct |
1 ms |
348 KB |
n = 8 |
13 |
Correct |
0 ms |
348 KB |
n = 8 |
14 |
Correct |
1 ms |
348 KB |
n = 8 |
15 |
Correct |
1 ms |
348 KB |
n = 8 |
16 |
Correct |
0 ms |
348 KB |
n = 8 |
17 |
Correct |
0 ms |
348 KB |
n = 8 |
18 |
Correct |
0 ms |
348 KB |
n = 8 |
19 |
Correct |
0 ms |
348 KB |
n = 3 |
20 |
Correct |
0 ms |
348 KB |
n = 7 |
21 |
Correct |
0 ms |
348 KB |
n = 8 |
22 |
Correct |
0 ms |
344 KB |
n = 8 |
23 |
Correct |
0 ms |
348 KB |
n = 8 |
24 |
Correct |
0 ms |
348 KB |
n = 8 |
25 |
Correct |
1 ms |
348 KB |
n = 8 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n = 2 |
2 |
Correct |
1 ms |
348 KB |
n = 2 |
3 |
Correct |
1 ms |
348 KB |
n = 2 |
4 |
Correct |
1 ms |
348 KB |
n = 2 |
5 |
Correct |
1 ms |
348 KB |
n = 2 |
6 |
Correct |
0 ms |
348 KB |
n = 2 |
7 |
Correct |
0 ms |
348 KB |
n = 3 |
8 |
Correct |
0 ms |
348 KB |
n = 3 |
9 |
Correct |
0 ms |
344 KB |
n = 3 |
10 |
Correct |
0 ms |
348 KB |
n = 8 |
11 |
Correct |
0 ms |
348 KB |
n = 8 |
12 |
Correct |
1 ms |
348 KB |
n = 8 |
13 |
Correct |
0 ms |
348 KB |
n = 8 |
14 |
Correct |
1 ms |
348 KB |
n = 8 |
15 |
Correct |
1 ms |
348 KB |
n = 8 |
16 |
Correct |
0 ms |
348 KB |
n = 8 |
17 |
Correct |
0 ms |
348 KB |
n = 8 |
18 |
Correct |
0 ms |
348 KB |
n = 8 |
19 |
Correct |
0 ms |
348 KB |
n = 3 |
20 |
Correct |
0 ms |
348 KB |
n = 7 |
21 |
Correct |
0 ms |
348 KB |
n = 8 |
22 |
Correct |
0 ms |
344 KB |
n = 8 |
23 |
Correct |
0 ms |
348 KB |
n = 8 |
24 |
Correct |
0 ms |
348 KB |
n = 8 |
25 |
Correct |
1 ms |
348 KB |
n = 8 |
26 |
Correct |
1 ms |
348 KB |
n = 8 |
27 |
Correct |
1 ms |
348 KB |
n = 8 |
28 |
Correct |
0 ms |
348 KB |
n = 8 |
29 |
Correct |
1 ms |
348 KB |
n = 16 |
30 |
Correct |
0 ms |
348 KB |
n = 16 |
31 |
Correct |
0 ms |
348 KB |
n = 16 |
32 |
Correct |
0 ms |
348 KB |
n = 16 |
33 |
Correct |
0 ms |
344 KB |
n = 16 |
34 |
Correct |
0 ms |
348 KB |
n = 16 |
35 |
Correct |
1 ms |
348 KB |
n = 16 |
36 |
Correct |
1 ms |
348 KB |
n = 15 |
37 |
Correct |
0 ms |
348 KB |
n = 8 |
38 |
Correct |
0 ms |
348 KB |
n = 16 |
39 |
Correct |
0 ms |
348 KB |
n = 16 |
40 |
Correct |
1 ms |
348 KB |
n = 9 |
41 |
Correct |
0 ms |
348 KB |
n = 16 |
42 |
Correct |
0 ms |
348 KB |
n = 16 |
43 |
Correct |
1 ms |
344 KB |
n = 16 |
44 |
Correct |
0 ms |
348 KB |
n = 9 |
45 |
Correct |
0 ms |
348 KB |
n = 15 |
46 |
Correct |
1 ms |
348 KB |
n = 16 |
47 |
Correct |
0 ms |
348 KB |
n = 16 |
48 |
Correct |
1 ms |
348 KB |
n = 16 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
607 ms |
53680 KB |
n = 199999 |
2 |
Correct |
689 ms |
58064 KB |
n = 199991 |
3 |
Correct |
636 ms |
57772 KB |
n = 199993 |
4 |
Correct |
412 ms |
45140 KB |
n = 152076 |
5 |
Correct |
218 ms |
27320 KB |
n = 93249 |
6 |
Correct |
447 ms |
49088 KB |
n = 199910 |
7 |
Correct |
570 ms |
56296 KB |
n = 199999 |
8 |
Correct |
466 ms |
50660 KB |
n = 199997 |
9 |
Correct |
515 ms |
50316 KB |
n = 171294 |
10 |
Correct |
348 ms |
44464 KB |
n = 140872 |
11 |
Correct |
491 ms |
49348 KB |
n = 199886 |
12 |
Correct |
544 ms |
57284 KB |
n = 199996 |
13 |
Correct |
514 ms |
52416 KB |
n = 200000 |
14 |
Correct |
499 ms |
57028 KB |
n = 199998 |
15 |
Correct |
530 ms |
55236 KB |
n = 200000 |
16 |
Correct |
546 ms |
57788 KB |
n = 199998 |
17 |
Correct |
561 ms |
61396 KB |
n = 200000 |
18 |
Correct |
564 ms |
56508 KB |
n = 190000 |
19 |
Correct |
477 ms |
54948 KB |
n = 177777 |
20 |
Correct |
218 ms |
29044 KB |
n = 100000 |
21 |
Correct |
695 ms |
58540 KB |
n = 200000 |
22 |
Correct |
565 ms |
59248 KB |
n = 200000 |
23 |
Correct |
634 ms |
57788 KB |
n = 200000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n = 2 |
2 |
Correct |
1 ms |
348 KB |
n = 2 |
3 |
Correct |
1 ms |
348 KB |
n = 2 |
4 |
Correct |
1 ms |
348 KB |
n = 2 |
5 |
Correct |
1 ms |
348 KB |
n = 2 |
6 |
Correct |
0 ms |
348 KB |
n = 2 |
7 |
Correct |
0 ms |
348 KB |
n = 3 |
8 |
Correct |
0 ms |
348 KB |
n = 3 |
9 |
Correct |
0 ms |
344 KB |
n = 3 |
10 |
Correct |
0 ms |
348 KB |
n = 8 |
11 |
Correct |
0 ms |
348 KB |
n = 8 |
12 |
Correct |
1 ms |
348 KB |
n = 8 |
13 |
Correct |
0 ms |
348 KB |
n = 8 |
14 |
Correct |
1 ms |
348 KB |
n = 8 |
15 |
Correct |
1 ms |
348 KB |
n = 8 |
16 |
Correct |
0 ms |
348 KB |
n = 8 |
17 |
Correct |
0 ms |
348 KB |
n = 8 |
18 |
Correct |
0 ms |
348 KB |
n = 8 |
19 |
Correct |
0 ms |
348 KB |
n = 3 |
20 |
Correct |
0 ms |
348 KB |
n = 7 |
21 |
Correct |
0 ms |
348 KB |
n = 8 |
22 |
Correct |
0 ms |
344 KB |
n = 8 |
23 |
Correct |
0 ms |
348 KB |
n = 8 |
24 |
Correct |
0 ms |
348 KB |
n = 8 |
25 |
Correct |
1 ms |
348 KB |
n = 8 |
26 |
Correct |
1 ms |
348 KB |
n = 8 |
27 |
Correct |
1 ms |
348 KB |
n = 8 |
28 |
Correct |
0 ms |
348 KB |
n = 8 |
29 |
Correct |
1 ms |
348 KB |
n = 16 |
30 |
Correct |
0 ms |
348 KB |
n = 16 |
31 |
Correct |
0 ms |
348 KB |
n = 16 |
32 |
Correct |
0 ms |
348 KB |
n = 16 |
33 |
Correct |
0 ms |
344 KB |
n = 16 |
34 |
Correct |
0 ms |
348 KB |
n = 16 |
35 |
Correct |
1 ms |
348 KB |
n = 16 |
36 |
Correct |
1 ms |
348 KB |
n = 15 |
37 |
Correct |
0 ms |
348 KB |
n = 8 |
38 |
Correct |
0 ms |
348 KB |
n = 16 |
39 |
Correct |
0 ms |
348 KB |
n = 16 |
40 |
Correct |
1 ms |
348 KB |
n = 9 |
41 |
Correct |
0 ms |
348 KB |
n = 16 |
42 |
Correct |
0 ms |
348 KB |
n = 16 |
43 |
Correct |
1 ms |
344 KB |
n = 16 |
44 |
Correct |
0 ms |
348 KB |
n = 9 |
45 |
Correct |
0 ms |
348 KB |
n = 15 |
46 |
Correct |
1 ms |
348 KB |
n = 16 |
47 |
Correct |
0 ms |
348 KB |
n = 16 |
48 |
Correct |
1 ms |
348 KB |
n = 16 |
49 |
Correct |
607 ms |
53680 KB |
n = 199999 |
50 |
Correct |
689 ms |
58064 KB |
n = 199991 |
51 |
Correct |
636 ms |
57772 KB |
n = 199993 |
52 |
Correct |
412 ms |
45140 KB |
n = 152076 |
53 |
Correct |
218 ms |
27320 KB |
n = 93249 |
54 |
Correct |
447 ms |
49088 KB |
n = 199910 |
55 |
Correct |
570 ms |
56296 KB |
n = 199999 |
56 |
Correct |
466 ms |
50660 KB |
n = 199997 |
57 |
Correct |
515 ms |
50316 KB |
n = 171294 |
58 |
Correct |
348 ms |
44464 KB |
n = 140872 |
59 |
Correct |
491 ms |
49348 KB |
n = 199886 |
60 |
Correct |
544 ms |
57284 KB |
n = 199996 |
61 |
Correct |
514 ms |
52416 KB |
n = 200000 |
62 |
Correct |
499 ms |
57028 KB |
n = 199998 |
63 |
Correct |
530 ms |
55236 KB |
n = 200000 |
64 |
Correct |
546 ms |
57788 KB |
n = 199998 |
65 |
Correct |
561 ms |
61396 KB |
n = 200000 |
66 |
Correct |
564 ms |
56508 KB |
n = 190000 |
67 |
Correct |
477 ms |
54948 KB |
n = 177777 |
68 |
Correct |
218 ms |
29044 KB |
n = 100000 |
69 |
Correct |
695 ms |
58540 KB |
n = 200000 |
70 |
Correct |
565 ms |
59248 KB |
n = 200000 |
71 |
Correct |
634 ms |
57788 KB |
n = 200000 |
72 |
Correct |
602 ms |
58036 KB |
n = 200000 |
73 |
Correct |
582 ms |
59080 KB |
n = 200000 |
74 |
Incorrect |
613 ms |
58708 KB |
answer is not correct: 66605227847128 instead of 66673947323864 |
75 |
Halted |
0 ms |
0 KB |
- |