#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.front()},{l0.back()})) {
if(!are_connected({l0.back()},{l1.back()})) {
std::reverse(l0.begin(),l0.end());
}
mergeLines();
return l0;
} else if(!are_connected({l1.front()},{l1.back()})) {
if(!are_connected({l0.back()},{l1.back()})) {
std::reverse(l1.begin(),l1.end());
}
mergeLines();
return l0;
} else if(!are_connected(l0,l1)) {
if(l0.size()>l1.size()) return l0;
else return l1;
} 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 |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
3 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
11 ms |
208 KB |
Output is correct |
4 |
Correct |
10 ms |
208 KB |
Output is correct |
5 |
Correct |
6 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
7 ms |
256 KB |
Output is correct |
4 |
Correct |
9 ms |
208 KB |
Output is correct |
5 |
Correct |
10 ms |
300 KB |
Output is correct |
6 |
Incorrect |
0 ms |
208 KB |
non-disjoint arrays |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
208 KB |
Output is correct |
2 |
Correct |
5 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
208 KB |
Output is correct |
4 |
Correct |
8 ms |
208 KB |
Output is correct |
5 |
Correct |
7 ms |
208 KB |
Output is correct |
6 |
Incorrect |
1 ms |
208 KB |
non-disjoint arrays |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
6 ms |
208 KB |
Output is correct |
3 |
Correct |
7 ms |
208 KB |
Output is correct |
4 |
Correct |
8 ms |
208 KB |
Output is correct |
5 |
Correct |
9 ms |
292 KB |
Output is correct |
6 |
Incorrect |
0 ms |
208 KB |
non-disjoint arrays |
7 |
Halted |
0 ms |
0 KB |
- |