#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
#define sz(x) (int)(x.size())
mt19937 rng(42);
vector<int> longest_trip(int N, int D){
(void)D;
vector<vector<int>> paths;
for(int i = 0; i < N; i++) paths.push_back({i});
auto doMerge = [&](int a, int b){
if(a > b) swap(a, b);
vector<int> va = paths[a], vb = paths[b];
paths.erase(paths.begin()+b);
paths.erase(paths.begin()+a);
reverse(va.begin(), va.end());
va.insert(va.end(), vb.begin(), vb.end());
paths.push_back(va);
};
while(sz(paths) >= 3){
vector<int> ord = {0, 1, 2};
shuffle(ord.begin(), ord.end(), rng);
if(are_connected({paths[ord[0]][0]}, {paths[ord[1]][0]})){
doMerge(ord[0], ord[1]);
}
else if(are_connected({paths[ord[0]][0]}, {paths[ord[2]][0]})){
doMerge(ord[0], ord[2]);
}
else doMerge(ord[1], ord[2]);
}
if(sz(paths) == 1) return paths[0];
if(!are_connected(paths[0], paths[1])){
return sz(paths[0]) > sz(paths[1]) ? paths[0] : paths[1];
}
for(int i = 0; i < 2; i++){
if(sz(paths[i]) < 3) continue;
if(!are_connected({paths[i][0]}, {paths[i].back()})){
if(are_connected({paths[i][0]}, {paths[i^1][0]})){
reverse(paths[i].begin(), paths[i].end());
}
paths[i].insert(paths[i].end(), paths[i^1].begin(), paths[i^1].end());
return paths[i];
}
}
auto prefix = [&](vector<int> v, int len){
vector<int> w;
for(int i = 0; i <= len; i++) w.push_back(v[i]);
return w;
};
int lo = 0, hi = sz(paths[0])-1;
while(lo < hi){
int m = (lo+hi)/2;
if(are_connected(prefix(paths[0], m), paths[1])) hi = m;
else lo = m+1;
}
int p1 = lo;
lo = 0, hi = sz(paths[1])-1;
while(lo < hi){
int m = (lo+hi)/2;
if(are_connected(prefix(paths[0], p1), prefix(paths[1], m))) hi = m;
else lo = m+1;
}
vector<int> res;
for(int i = 1; i <= sz(paths[0]); i++){
res.push_back(paths[0][(p1+i)%sz(paths[0])]);
}
for(int i = 0; i < sz(paths[1]); i++){
res.push_back(paths[1][(lo+i)%sz(paths[1])]);
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
4 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
14 ms |
208 KB |
Output is correct |
3 |
Correct |
12 ms |
316 KB |
Output is correct |
4 |
Correct |
9 ms |
320 KB |
Output is correct |
5 |
Correct |
10 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
256 KB |
Output is correct |
2 |
Correct |
11 ms |
208 KB |
Output is correct |
3 |
Correct |
10 ms |
208 KB |
Output is correct |
4 |
Correct |
12 ms |
312 KB |
Output is correct |
5 |
Correct |
12 ms |
328 KB |
Output is correct |
6 |
Correct |
10 ms |
208 KB |
Output is correct |
7 |
Correct |
11 ms |
208 KB |
Output is correct |
8 |
Correct |
13 ms |
208 KB |
Output is correct |
9 |
Correct |
7 ms |
208 KB |
Output is correct |
10 |
Correct |
9 ms |
316 KB |
Output is correct |
11 |
Correct |
10 ms |
324 KB |
Output is correct |
12 |
Correct |
11 ms |
208 KB |
Output is correct |
13 |
Correct |
10 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
9 ms |
208 KB |
Output is correct |
4 |
Correct |
9 ms |
320 KB |
Output is correct |
5 |
Correct |
11 ms |
320 KB |
Output is correct |
6 |
Correct |
9 ms |
256 KB |
Output is correct |
7 |
Correct |
14 ms |
220 KB |
Output is correct |
8 |
Correct |
10 ms |
208 KB |
Output is correct |
9 |
Correct |
11 ms |
208 KB |
Output is correct |
10 |
Correct |
11 ms |
316 KB |
Output is correct |
11 |
Correct |
7 ms |
336 KB |
Output is correct |
12 |
Correct |
11 ms |
336 KB |
Output is correct |
13 |
Correct |
11 ms |
208 KB |
Output is correct |
14 |
Correct |
10 ms |
208 KB |
Output is correct |
15 |
Correct |
13 ms |
208 KB |
Output is correct |
16 |
Correct |
14 ms |
300 KB |
Output is correct |
17 |
Correct |
13 ms |
208 KB |
Output is correct |
18 |
Correct |
10 ms |
208 KB |
Output is correct |
19 |
Correct |
11 ms |
208 KB |
Output is correct |
20 |
Correct |
12 ms |
308 KB |
Output is correct |
21 |
Correct |
12 ms |
328 KB |
Output is correct |
22 |
Correct |
11 ms |
332 KB |
Output is correct |
23 |
Correct |
12 ms |
328 KB |
Output is correct |
24 |
Correct |
12 ms |
336 KB |
Output is correct |
25 |
Correct |
6 ms |
208 KB |
Output is correct |
26 |
Correct |
7 ms |
208 KB |
Output is correct |
27 |
Correct |
10 ms |
208 KB |
Output is correct |
28 |
Correct |
8 ms |
208 KB |
Output is correct |
29 |
Correct |
15 ms |
208 KB |
Output is correct |
30 |
Correct |
14 ms |
208 KB |
Output is correct |
31 |
Correct |
15 ms |
208 KB |
Output is correct |
32 |
Correct |
16 ms |
208 KB |
Output is correct |
33 |
Correct |
15 ms |
320 KB |
Output is correct |
34 |
Correct |
12 ms |
208 KB |
Output is correct |
35 |
Correct |
11 ms |
208 KB |
Output is correct |
36 |
Correct |
13 ms |
332 KB |
Output is correct |
37 |
Correct |
10 ms |
208 KB |
Output is correct |
38 |
Correct |
15 ms |
316 KB |
Output is correct |
39 |
Correct |
12 ms |
336 KB |
Output is correct |
40 |
Correct |
13 ms |
208 KB |
Output is correct |
41 |
Correct |
16 ms |
324 KB |
Output is correct |
42 |
Correct |
14 ms |
336 KB |
Output is correct |
43 |
Correct |
16 ms |
320 KB |
Output is correct |
44 |
Correct |
9 ms |
332 KB |
Output is correct |
45 |
Correct |
15 ms |
208 KB |
Output is correct |
46 |
Correct |
15 ms |
208 KB |
Output is correct |
47 |
Correct |
16 ms |
208 KB |
Output is correct |
48 |
Correct |
10 ms |
208 KB |
Output is correct |
49 |
Correct |
15 ms |
256 KB |
Output is correct |
50 |
Correct |
13 ms |
208 KB |
Output is correct |
51 |
Correct |
12 ms |
208 KB |
Output is correct |
52 |
Correct |
8 ms |
308 KB |
Output is correct |
53 |
Correct |
13 ms |
236 KB |
Output is correct |
54 |
Correct |
13 ms |
208 KB |
Output is correct |
55 |
Correct |
9 ms |
208 KB |
Output is correct |
56 |
Correct |
10 ms |
332 KB |
Output is correct |
57 |
Correct |
13 ms |
208 KB |
Output is correct |
58 |
Correct |
14 ms |
208 KB |
Output is correct |
59 |
Correct |
17 ms |
208 KB |
Output is correct |
60 |
Correct |
13 ms |
208 KB |
Output is correct |
61 |
Correct |
12 ms |
332 KB |
Output is correct |
62 |
Correct |
12 ms |
320 KB |
Output is correct |
63 |
Correct |
13 ms |
208 KB |
Output is correct |
64 |
Correct |
12 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
208 KB |
Output is correct |
2 |
Correct |
12 ms |
208 KB |
Output is correct |
3 |
Correct |
11 ms |
208 KB |
Output is correct |
4 |
Correct |
8 ms |
208 KB |
Output is correct |
5 |
Correct |
8 ms |
336 KB |
Output is correct |
6 |
Correct |
8 ms |
208 KB |
Output is correct |
7 |
Correct |
11 ms |
260 KB |
Output is correct |
8 |
Correct |
8 ms |
208 KB |
Output is correct |
9 |
Correct |
11 ms |
208 KB |
Output is correct |
10 |
Correct |
8 ms |
336 KB |
Output is correct |
11 |
Correct |
8 ms |
208 KB |
Output is correct |
12 |
Correct |
11 ms |
208 KB |
Output is correct |
13 |
Correct |
7 ms |
208 KB |
Output is correct |
14 |
Correct |
8 ms |
208 KB |
Output is correct |
15 |
Correct |
9 ms |
208 KB |
Output is correct |
16 |
Correct |
15 ms |
208 KB |
Output is correct |
17 |
Correct |
13 ms |
256 KB |
Output is correct |
18 |
Correct |
12 ms |
208 KB |
Output is correct |
19 |
Correct |
10 ms |
208 KB |
Output is correct |
20 |
Correct |
10 ms |
208 KB |
Output is correct |
21 |
Correct |
11 ms |
208 KB |
Output is correct |
22 |
Correct |
9 ms |
232 KB |
Output is correct |
23 |
Correct |
9 ms |
208 KB |
Output is correct |
24 |
Correct |
11 ms |
208 KB |
Output is correct |
25 |
Correct |
10 ms |
208 KB |
Output is correct |
26 |
Correct |
10 ms |
208 KB |
Output is correct |
27 |
Correct |
9 ms |
208 KB |
Output is correct |
28 |
Correct |
8 ms |
208 KB |
Output is correct |
29 |
Correct |
10 ms |
208 KB |
Output is correct |
30 |
Correct |
8 ms |
208 KB |
Output is correct |
31 |
Correct |
10 ms |
208 KB |
Output is correct |
32 |
Correct |
10 ms |
208 KB |
Output is correct |
33 |
Correct |
17 ms |
208 KB |
Output is correct |
34 |
Correct |
11 ms |
208 KB |
Output is correct |
35 |
Correct |
11 ms |
208 KB |
Output is correct |
36 |
Correct |
15 ms |
208 KB |
Output is correct |
37 |
Correct |
10 ms |
208 KB |
Output is correct |
38 |
Correct |
15 ms |
208 KB |
Output is correct |
39 |
Correct |
13 ms |
260 KB |
Output is correct |
40 |
Correct |
13 ms |
208 KB |
Output is correct |
41 |
Correct |
12 ms |
208 KB |
Output is correct |
42 |
Correct |
9 ms |
208 KB |
Output is correct |
43 |
Correct |
9 ms |
320 KB |
Output is correct |
44 |
Correct |
12 ms |
324 KB |
Output is correct |
45 |
Correct |
11 ms |
336 KB |
Output is correct |
46 |
Correct |
9 ms |
336 KB |
Output is correct |
47 |
Partially correct |
13 ms |
344 KB |
Output is partially correct |
48 |
Correct |
11 ms |
320 KB |
Output is correct |
49 |
Correct |
11 ms |
208 KB |
Output is correct |
50 |
Correct |
13 ms |
208 KB |
Output is correct |
51 |
Correct |
16 ms |
208 KB |
Output is correct |
52 |
Correct |
8 ms |
208 KB |
Output is correct |
53 |
Correct |
14 ms |
320 KB |
Output is correct |
54 |
Correct |
16 ms |
332 KB |
Output is correct |
55 |
Correct |
14 ms |
384 KB |
Output is correct |
56 |
Correct |
8 ms |
208 KB |
Output is correct |
57 |
Correct |
10 ms |
208 KB |
Output is correct |
58 |
Correct |
13 ms |
336 KB |
Output is correct |
59 |
Correct |
10 ms |
320 KB |
Output is correct |
60 |
Correct |
11 ms |
336 KB |
Output is correct |
61 |
Partially correct |
13 ms |
208 KB |
Output is partially correct |
62 |
Correct |
10 ms |
208 KB |
Output is correct |
63 |
Correct |
14 ms |
208 KB |
Output is correct |
64 |
Correct |
11 ms |
320 KB |
Output is correct |
65 |
Partially correct |
17 ms |
208 KB |
Output is partially correct |
66 |
Partially correct |
17 ms |
208 KB |
Output is partially correct |
67 |
Partially correct |
11 ms |
320 KB |
Output is partially correct |
68 |
Partially correct |
17 ms |
208 KB |
Output is partially correct |
69 |
Partially correct |
18 ms |
208 KB |
Output is partially correct |
70 |
Partially correct |
12 ms |
208 KB |
Output is partially correct |
71 |
Correct |
15 ms |
208 KB |
Output is correct |
72 |
Correct |
13 ms |
208 KB |
Output is correct |
73 |
Partially correct |
16 ms |
208 KB |
Output is partially correct |
74 |
Partially correct |
13 ms |
328 KB |
Output is partially correct |
75 |
Partially correct |
12 ms |
336 KB |
Output is partially correct |
76 |
Partially correct |
14 ms |
208 KB |
Output is partially correct |
77 |
Correct |
11 ms |
208 KB |
Output is correct |
78 |
Correct |
12 ms |
336 KB |
Output is correct |
79 |
Partially correct |
11 ms |
208 KB |
Output is partially correct |
80 |
Partially correct |
11 ms |
324 KB |
Output is partially correct |
81 |
Partially correct |
12 ms |
324 KB |
Output is partially correct |
82 |
Partially correct |
11 ms |
208 KB |
Output is partially correct |