#include "longesttrip.h"
#include <functional>
#include <vector>
std::vector<int> longest_trip(int N, int D) {
//Invariant: l0.back() and l1.back() are NOT connected.
std::vector<int> l0,l1;
auto mergeLines = [&l0, &l1]()->void{
while(!l1.empty()) {
l0.push_back(l1.back());
l1.pop_back();
}
return;
};
auto q = [](int u, int v)->bool{
return are_connected({u},{v});
};
l0.push_back(0);
int u=1;
while(u<N) {
if(l1.empty()) {
(q(l0.back(),u)?l0:l1).push_back(u);
u++;
} else if(u==N-1) {
if(!q(l0.back(),u)) l1.push_back(u);
else {
l0.push_back(u);
if(q(l1.back(),u)) {
mergeLines();
}
}
u++;
} else {
int v=u+1;
bool b0 = q(l0.back(),u), b1 = q(l1.back(),v), b2 = q(u,v);
if(b0&&b1) {
l0.push_back(u);
l1.push_back(v);
if(b2) mergeLines();
} else if(!b0&&!b1) {
l0.push_back(v);
l1.push_back(u);
if(b2) mergeLines();
} else if(b0&&!b1) {
if(b2) {
l0.push_back(u);
l0.push_back(v);
} else {
l1.push_back(u);
l0.push_back(v);
}
} else {
if(b2) {
l1.push_back(v);
l1.push_back(u);
} else {
l0.push_back(v);
l1.push_back(u);
}
}
u+=2;
}
}
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()}))) {
std::reverse(l0.begin(),l0.end());
mergeLines();
return l0;
} else if(l1.size()>1&&(!are_connected({l1.front()},{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 |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
600 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
600 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
596 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
600 KB |
Output is correct |
14 |
Correct |
10 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Correct |
10 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
5 ms |
344 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
5 ms |
420 KB |
Output is correct |
21 |
Correct |
5 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
344 KB |
Output is correct |
23 |
Correct |
5 ms |
428 KB |
Output is correct |
24 |
Correct |
5 ms |
344 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
344 KB |
Output is correct |
27 |
Correct |
5 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
9 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
6 ms |
344 KB |
Output is correct |
33 |
Correct |
5 ms |
344 KB |
Output is correct |
34 |
Correct |
6 ms |
344 KB |
Output is correct |
35 |
Correct |
6 ms |
344 KB |
Output is correct |
36 |
Correct |
9 ms |
344 KB |
Output is correct |
37 |
Correct |
7 ms |
600 KB |
Output is correct |
38 |
Correct |
7 ms |
344 KB |
Output is correct |
39 |
Correct |
7 ms |
344 KB |
Output is correct |
40 |
Correct |
7 ms |
344 KB |
Output is correct |
41 |
Correct |
7 ms |
600 KB |
Output is correct |
42 |
Correct |
8 ms |
344 KB |
Output is correct |
43 |
Correct |
6 ms |
344 KB |
Output is correct |
44 |
Correct |
7 ms |
344 KB |
Output is correct |
45 |
Correct |
7 ms |
344 KB |
Output is correct |
46 |
Correct |
10 ms |
344 KB |
Output is correct |
47 |
Correct |
8 ms |
344 KB |
Output is correct |
48 |
Correct |
6 ms |
344 KB |
Output is correct |
49 |
Correct |
7 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
424 KB |
Output is correct |
51 |
Correct |
7 ms |
424 KB |
Output is correct |
52 |
Correct |
7 ms |
356 KB |
Output is correct |
53 |
Correct |
6 ms |
344 KB |
Output is correct |
54 |
Correct |
6 ms |
600 KB |
Output is correct |
55 |
Correct |
7 ms |
344 KB |
Output is correct |
56 |
Correct |
5 ms |
424 KB |
Output is correct |
57 |
Correct |
7 ms |
856 KB |
Output is correct |
58 |
Correct |
7 ms |
600 KB |
Output is correct |
59 |
Correct |
7 ms |
600 KB |
Output is correct |
60 |
Correct |
7 ms |
424 KB |
Output is correct |
61 |
Correct |
7 ms |
344 KB |
Output is correct |
62 |
Correct |
7 ms |
604 KB |
Output is correct |
63 |
Correct |
7 ms |
428 KB |
Output is correct |
64 |
Correct |
6 ms |
428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
600 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
344 KB |
Output is correct |
17 |
Correct |
7 ms |
344 KB |
Output is correct |
18 |
Correct |
7 ms |
344 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
5 ms |
600 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
7 ms |
344 KB |
Output is correct |
23 |
Correct |
8 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
344 KB |
Output is correct |
25 |
Correct |
6 ms |
344 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
7 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
10 ms |
504 KB |
Output is correct |
36 |
Correct |
8 ms |
344 KB |
Output is correct |
37 |
Correct |
5 ms |
424 KB |
Output is correct |
38 |
Correct |
7 ms |
424 KB |
Output is correct |
39 |
Correct |
7 ms |
344 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
7 ms |
344 KB |
Output is correct |
42 |
Correct |
7 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
344 KB |
Output is correct |
44 |
Correct |
5 ms |
344 KB |
Output is correct |
45 |
Correct |
5 ms |
428 KB |
Output is correct |
46 |
Correct |
5 ms |
344 KB |
Output is correct |
47 |
Correct |
5 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
344 KB |
Output is correct |
49 |
Correct |
6 ms |
344 KB |
Output is correct |
50 |
Correct |
7 ms |
600 KB |
Output is correct |
51 |
Correct |
7 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
6 ms |
600 KB |
Output is correct |
54 |
Correct |
6 ms |
344 KB |
Output is correct |
55 |
Correct |
8 ms |
600 KB |
Output is correct |
56 |
Correct |
8 ms |
600 KB |
Output is correct |
57 |
Correct |
6 ms |
344 KB |
Output is correct |
58 |
Correct |
10 ms |
420 KB |
Output is correct |
59 |
Correct |
8 ms |
600 KB |
Output is correct |
60 |
Correct |
6 ms |
344 KB |
Output is correct |
61 |
Correct |
8 ms |
600 KB |
Output is correct |
62 |
Correct |
5 ms |
432 KB |
Output is correct |
63 |
Correct |
7 ms |
600 KB |
Output is correct |
64 |
Correct |
7 ms |
604 KB |
Output is correct |
65 |
Correct |
6 ms |
600 KB |
Output is correct |
66 |
Correct |
6 ms |
600 KB |
Output is correct |
67 |
Correct |
7 ms |
344 KB |
Output is correct |
68 |
Correct |
8 ms |
424 KB |
Output is correct |
69 |
Correct |
6 ms |
604 KB |
Output is correct |
70 |
Correct |
6 ms |
600 KB |
Output is correct |
71 |
Correct |
6 ms |
600 KB |
Output is correct |
72 |
Correct |
6 ms |
600 KB |
Output is correct |
73 |
Correct |
7 ms |
420 KB |
Output is correct |
74 |
Correct |
7 ms |
428 KB |
Output is correct |
75 |
Correct |
8 ms |
504 KB |
Output is correct |
76 |
Correct |
8 ms |
428 KB |
Output is correct |
77 |
Correct |
6 ms |
600 KB |
Output is correct |
78 |
Correct |
6 ms |
600 KB |
Output is correct |
79 |
Correct |
8 ms |
604 KB |
Output is correct |
80 |
Correct |
7 ms |
600 KB |
Output is correct |
81 |
Correct |
8 ms |
424 KB |
Output is correct |
82 |
Correct |
7 ms |
600 KB |
Output is correct |