#include "longesttrip.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
std::vector<int> longest_trip(int N, int D) {
std::vector<int> l0,l1;
auto mergeLines = [&l0, &l1]()->void{
while(!l1.empty()) {
l0.push_back(l1.back());
l1.pop_back();
}
return;
};
std::vector<int> p(N);
std::iota(p.begin(),p.end(),0);
std::srand(2023);
std::random_shuffle(p.begin(),p.end());
l0.push_back(p[0]);
for(int i = 1; i < N; i++) {
int u = p[i];
if(are_connected({l0.back()},{u})) {
l0.push_back(u);
} else if(l1.empty() || are_connected({l1.back()},{u})) {
l1.push_back(u);
} else {
mergeLines();
i--; //Restart this loop
}
}
if(l1.empty()) return l0;
else if(!are_connected(l0,l1)) {
if(l0.size()>l1.size()) return l0;
else return l1;
} else if(l0.size()>1&&(!are_connected({l0.front()},{l0.back()}))) {
if(!are_connected({l0.back()},{l1.back()})) {
std::reverse(l0.begin(),l0.end());
}
mergeLines();
return l0;
} else if(l1.size()>1&&(!are_connected({l1.front()},{l1.back()}))) {
if(!are_connected({l0.back()},{l1.back()})) {
std::reverse(l1.begin(),l1.end());
}
mergeLines();
return l0;
} else {
std::vector<int> le(l0), ri(l1);
for(int rep = 0; rep < 2; rep++) {
while(le.size()>1) {
std::vector<int> le0,le1;
for(int i = 0; i < int(le.size()); i++) {
(i%2?le0:le1).push_back(le[i]);
}
if(are_connected(le0,ri)) std::swap(le,le0);
else std::swap(le,le1);
}
swap(le,ri);
}
int lei = 0, rii = 0;
while(l0[lei] != le[0]) lei++;
while(l1[rii] != ri[0]) rii++;
std::vector<int> ans;
for(int i = 0; i < int(l0.size()); i++) {
ans.push_back(l0[(lei+i+1)%l0.size()]);
}
for(int i = 0; i < int(l1.size()); i++) {
ans.push_back(l1[(rii+i)%l1.size()]);
}
return ans;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
3 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
208 KB |
Output is correct |
2 |
Correct |
8 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
208 KB |
Output is correct |
4 |
Correct |
9 ms |
208 KB |
Output is correct |
5 |
Correct |
8 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
208 KB |
Output is correct |
2 |
Correct |
7 ms |
208 KB |
Output is correct |
3 |
Correct |
10 ms |
208 KB |
Output is correct |
4 |
Correct |
7 ms |
208 KB |
Output is correct |
5 |
Correct |
5 ms |
208 KB |
Output is correct |
6 |
Correct |
11 ms |
208 KB |
Output is correct |
7 |
Correct |
9 ms |
208 KB |
Output is correct |
8 |
Correct |
8 ms |
208 KB |
Output is correct |
9 |
Correct |
8 ms |
208 KB |
Output is correct |
10 |
Correct |
9 ms |
208 KB |
Output is correct |
11 |
Correct |
7 ms |
208 KB |
Output is correct |
12 |
Correct |
7 ms |
208 KB |
Output is correct |
13 |
Correct |
9 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
8 ms |
208 KB |
Output is correct |
3 |
Correct |
7 ms |
208 KB |
Output is correct |
4 |
Correct |
7 ms |
208 KB |
Output is correct |
5 |
Correct |
10 ms |
208 KB |
Output is correct |
6 |
Correct |
12 ms |
208 KB |
Output is correct |
7 |
Correct |
9 ms |
208 KB |
Output is correct |
8 |
Correct |
7 ms |
208 KB |
Output is correct |
9 |
Correct |
7 ms |
208 KB |
Output is correct |
10 |
Correct |
9 ms |
208 KB |
Output is correct |
11 |
Correct |
6 ms |
208 KB |
Output is correct |
12 |
Correct |
9 ms |
304 KB |
Output is correct |
13 |
Correct |
7 ms |
296 KB |
Output is correct |
14 |
Correct |
11 ms |
208 KB |
Output is correct |
15 |
Correct |
11 ms |
208 KB |
Output is correct |
16 |
Correct |
14 ms |
208 KB |
Output is correct |
17 |
Correct |
13 ms |
208 KB |
Output is correct |
18 |
Correct |
9 ms |
300 KB |
Output is correct |
19 |
Correct |
8 ms |
304 KB |
Output is correct |
20 |
Correct |
5 ms |
308 KB |
Output is correct |
21 |
Correct |
6 ms |
312 KB |
Output is correct |
22 |
Correct |
11 ms |
296 KB |
Output is correct |
23 |
Correct |
8 ms |
208 KB |
Output is correct |
24 |
Correct |
10 ms |
208 KB |
Output is correct |
25 |
Correct |
10 ms |
208 KB |
Output is correct |
26 |
Correct |
13 ms |
208 KB |
Output is correct |
27 |
Correct |
6 ms |
208 KB |
Output is correct |
28 |
Correct |
10 ms |
208 KB |
Output is correct |
29 |
Correct |
8 ms |
208 KB |
Output is correct |
30 |
Correct |
8 ms |
208 KB |
Output is correct |
31 |
Correct |
14 ms |
208 KB |
Output is correct |
32 |
Correct |
12 ms |
208 KB |
Output is correct |
33 |
Correct |
7 ms |
304 KB |
Output is correct |
34 |
Correct |
7 ms |
296 KB |
Output is correct |
35 |
Correct |
7 ms |
300 KB |
Output is correct |
36 |
Correct |
8 ms |
296 KB |
Output is correct |
37 |
Correct |
7 ms |
208 KB |
Output is correct |
38 |
Correct |
10 ms |
208 KB |
Output is correct |
39 |
Correct |
10 ms |
208 KB |
Output is correct |
40 |
Correct |
10 ms |
208 KB |
Output is correct |
41 |
Correct |
9 ms |
208 KB |
Output is correct |
42 |
Correct |
12 ms |
300 KB |
Output is correct |
43 |
Correct |
10 ms |
208 KB |
Output is correct |
44 |
Correct |
13 ms |
208 KB |
Output is correct |
45 |
Correct |
16 ms |
208 KB |
Output is correct |
46 |
Correct |
19 ms |
208 KB |
Output is correct |
47 |
Correct |
9 ms |
208 KB |
Output is correct |
48 |
Correct |
12 ms |
208 KB |
Output is correct |
49 |
Correct |
9 ms |
208 KB |
Output is correct |
50 |
Correct |
10 ms |
208 KB |
Output is correct |
51 |
Correct |
10 ms |
208 KB |
Output is correct |
52 |
Correct |
11 ms |
208 KB |
Output is correct |
53 |
Correct |
8 ms |
300 KB |
Output is correct |
54 |
Correct |
9 ms |
208 KB |
Output is correct |
55 |
Correct |
9 ms |
208 KB |
Output is correct |
56 |
Correct |
6 ms |
208 KB |
Output is correct |
57 |
Correct |
6 ms |
208 KB |
Output is correct |
58 |
Correct |
12 ms |
208 KB |
Output is correct |
59 |
Correct |
11 ms |
304 KB |
Output is correct |
60 |
Correct |
10 ms |
208 KB |
Output is correct |
61 |
Correct |
11 ms |
208 KB |
Output is correct |
62 |
Correct |
16 ms |
304 KB |
Output is correct |
63 |
Correct |
16 ms |
300 KB |
Output is correct |
64 |
Correct |
12 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
208 KB |
Output is correct |
2 |
Correct |
6 ms |
208 KB |
Output is correct |
3 |
Correct |
9 ms |
208 KB |
Output is correct |
4 |
Correct |
7 ms |
208 KB |
Output is correct |
5 |
Correct |
9 ms |
208 KB |
Output is correct |
6 |
Correct |
11 ms |
208 KB |
Output is correct |
7 |
Correct |
10 ms |
208 KB |
Output is correct |
8 |
Correct |
5 ms |
208 KB |
Output is correct |
9 |
Correct |
6 ms |
208 KB |
Output is correct |
10 |
Correct |
6 ms |
208 KB |
Output is correct |
11 |
Correct |
5 ms |
284 KB |
Output is correct |
12 |
Correct |
5 ms |
208 KB |
Output is correct |
13 |
Correct |
7 ms |
296 KB |
Output is correct |
14 |
Correct |
11 ms |
208 KB |
Output is correct |
15 |
Correct |
11 ms |
208 KB |
Output is correct |
16 |
Correct |
10 ms |
208 KB |
Output is correct |
17 |
Correct |
9 ms |
208 KB |
Output is correct |
18 |
Correct |
8 ms |
208 KB |
Output is correct |
19 |
Correct |
5 ms |
296 KB |
Output is correct |
20 |
Correct |
8 ms |
288 KB |
Output is correct |
21 |
Correct |
10 ms |
208 KB |
Output is correct |
22 |
Correct |
9 ms |
208 KB |
Output is correct |
23 |
Correct |
10 ms |
208 KB |
Output is correct |
24 |
Correct |
9 ms |
208 KB |
Output is correct |
25 |
Correct |
8 ms |
208 KB |
Output is correct |
26 |
Correct |
6 ms |
208 KB |
Output is correct |
27 |
Correct |
12 ms |
208 KB |
Output is correct |
28 |
Correct |
8 ms |
208 KB |
Output is correct |
29 |
Correct |
7 ms |
208 KB |
Output is correct |
30 |
Correct |
7 ms |
208 KB |
Output is correct |
31 |
Correct |
7 ms |
304 KB |
Output is correct |
32 |
Correct |
13 ms |
208 KB |
Output is correct |
33 |
Correct |
16 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 |
10 ms |
208 KB |
Output is correct |
37 |
Correct |
8 ms |
208 KB |
Output is correct |
38 |
Correct |
10 ms |
208 KB |
Output is correct |
39 |
Correct |
10 ms |
208 KB |
Output is correct |
40 |
Correct |
8 ms |
208 KB |
Output is correct |
41 |
Correct |
7 ms |
208 KB |
Output is correct |
42 |
Correct |
8 ms |
208 KB |
Output is correct |
43 |
Correct |
8 ms |
300 KB |
Output is correct |
44 |
Correct |
7 ms |
208 KB |
Output is correct |
45 |
Correct |
7 ms |
208 KB |
Output is correct |
46 |
Correct |
7 ms |
304 KB |
Output is correct |
47 |
Correct |
9 ms |
208 KB |
Output is correct |
48 |
Correct |
5 ms |
208 KB |
Output is correct |
49 |
Correct |
6 ms |
208 KB |
Output is correct |
50 |
Partially correct |
9 ms |
208 KB |
Output is partially correct |
51 |
Partially correct |
13 ms |
292 KB |
Output is partially correct |
52 |
Correct |
11 ms |
208 KB |
Output is correct |
53 |
Correct |
9 ms |
208 KB |
Output is correct |
54 |
Correct |
11 ms |
208 KB |
Output is correct |
55 |
Correct |
10 ms |
208 KB |
Output is correct |
56 |
Correct |
6 ms |
208 KB |
Output is correct |
57 |
Correct |
7 ms |
208 KB |
Output is correct |
58 |
Correct |
9 ms |
208 KB |
Output is correct |
59 |
Partially correct |
11 ms |
208 KB |
Output is partially correct |
60 |
Correct |
12 ms |
208 KB |
Output is correct |
61 |
Correct |
9 ms |
208 KB |
Output is correct |
62 |
Correct |
9 ms |
208 KB |
Output is correct |
63 |
Correct |
5 ms |
208 KB |
Output is correct |
64 |
Partially correct |
7 ms |
300 KB |
Output is partially correct |
65 |
Partially correct |
11 ms |
304 KB |
Output is partially correct |
66 |
Partially correct |
13 ms |
336 KB |
Output is partially correct |
67 |
Correct |
12 ms |
208 KB |
Output is correct |
68 |
Correct |
11 ms |
208 KB |
Output is correct |
69 |
Partially correct |
12 ms |
208 KB |
Output is partially correct |
70 |
Partially correct |
13 ms |
304 KB |
Output is partially correct |
71 |
Partially correct |
9 ms |
208 KB |
Output is partially correct |
72 |
Partially correct |
11 ms |
208 KB |
Output is partially correct |
73 |
Correct |
13 ms |
208 KB |
Output is correct |
74 |
Partially correct |
12 ms |
208 KB |
Output is partially correct |
75 |
Correct |
11 ms |
208 KB |
Output is correct |
76 |
Correct |
11 ms |
208 KB |
Output is correct |
77 |
Correct |
7 ms |
208 KB |
Output is correct |
78 |
Correct |
9 ms |
296 KB |
Output is correct |
79 |
Partially correct |
10 ms |
208 KB |
Output is partially correct |
80 |
Partially correct |
9 ms |
304 KB |
Output is partially correct |
81 |
Correct |
10 ms |
336 KB |
Output is correct |
82 |
Correct |
13 ms |
304 KB |
Output is correct |