#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(u);
l1.push_back(v);
} else {
l1.push_back(u);
l0.push_back(v);
}
}
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()}))) {
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 |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
420 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 |
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 |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 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 |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
5 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 |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
352 KB |
Output is correct |
14 |
Correct |
7 ms |
356 KB |
Output is correct |
15 |
Correct |
9 ms |
356 KB |
Output is correct |
16 |
Incorrect |
0 ms |
356 KB |
Incorrect |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
608 KB |
Output is correct |
2 |
Correct |
4 ms |
356 KB |
Output is correct |
3 |
Correct |
4 ms |
356 KB |
Output is correct |
4 |
Correct |
5 ms |
356 KB |
Output is correct |
5 |
Correct |
5 ms |
612 KB |
Output is correct |
6 |
Correct |
6 ms |
356 KB |
Output is correct |
7 |
Correct |
6 ms |
356 KB |
Output is correct |
8 |
Correct |
5 ms |
356 KB |
Output is correct |
9 |
Correct |
4 ms |
356 KB |
Output is correct |
10 |
Correct |
5 ms |
612 KB |
Output is correct |
11 |
Correct |
4 ms |
356 KB |
Output is correct |
12 |
Correct |
5 ms |
356 KB |
Output is correct |
13 |
Correct |
5 ms |
600 KB |
Output is correct |
14 |
Correct |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
6 ms |
340 KB |
Output is correct |
16 |
Incorrect |
1 ms |
504 KB |
Incorrect |
17 |
Halted |
0 ms |
0 KB |
- |