#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
int find_connected(vector<int> v, vector<int> arr) {
if (arr.size() == 0 || !are_connected(v, arr)) return -1;
int s = 0, e = arr.size() - 1;
while (s < e) {
int m = (s + e) / 2;
vector<int> temp;
for (int i = s; i <= m; i++) temp.push_back(arr[i]);
if (are_connected(v, temp)) e = m;
else s = m + 1;
}
return arr[s];
}
int find_connected_idx(vector<int> v, vector<int> arr) {
if (arr.size() == 0 || !are_connected(v, arr)) return -1;
int s = 0, e = arr.size() - 1;
while (s < e) {
int m = (s + e) / 2;
vector<int> temp;
for (int i = s; i <= m; i++) temp.push_back(arr[i]);
if (are_connected(v, temp)) e = m;
else s = m + 1;
}
return s;
}
vector<int> longest_trip(int N, int D) {
int n = N;
vector<int> res;
int ch[256]; for (int i = 0; i < n; i++) ch[i] = 0;
vector<int> p1, p2;
p1.push_back(0);
if (are_connected({0}, {1})) p1.push_back(1);
else p2.push_back(1);
for (int i = 2; i < n; i += 2) {
if (are_connected({p1.back()}, {i})) swap(p1, p2);
p1.push_back(i);
if (i == n - 1) continue;
if (are_connected({i}, {i + 1})) {
p1.push_back(i + 1);
if (p2.size() == 0 || are_connected({i + 1}, {p2.back()})) {
for (int j = p2.size() - 1; j >= 0; j--) p1.push_back(p2[j]);
p2.clear();
}
}
else {
if (p2.size() == 0 || are_connected({i + 1}, {p2.back()})) p2.push_back(i + 1);
else {
p1.pop_back();
p1.push_back(i + 1);
p2.push_back(i);
}
}
}
bool connected = false;
if (p1.size() > 0 && p2.size() > 0) connected = are_connected(p1, p2);
if (connected) {
if (are_connected({p1.front()}, {p2.front()})) {
reverse(p1.begin(), p1.end());
for (int i = 0; i < p1.size(); i++) res.push_back(p1[i]);
for (int i = 0; i < p2.size(); i++) res.push_back(p2[i]);
return res;
}
if (are_connected({p1.front()}, {p2.back()})) {
reverse(p1.begin(), p1.end());
reverse(p2.begin(), p2.end());
for (int i = 0; i < p1.size(); i++) res.push_back(p1[i]);
for (int i = 0; i < p2.size(); i++) res.push_back(p2[i]);
return res;
}
if (are_connected({p1.back()}, {p2.front()})) {
for (int i = 0; i < p1.size(); i++) res.push_back(p1[i]);
for (int i = 0; i < p2.size(); i++) res.push_back(p2[i]);
return res;
}
if (are_connected({p1.back()}, {p2.back()})) {
reverse(p2.begin(), p2.end());
for (int i = 0; i < p1.size(); i++) res.push_back(p1[i]);
for (int i = 0; i < p2.size(); i++) res.push_back(p2[i]);
return res;
}
int a = find_connected_idx(p1, p2);
int b = find_connected_idx({p2[a]}, p1); //p2[a] - p1[b]
for (int i = b + 1; i < p1.size(); i++) res.push_back(p1[i]);
for (int i = 0; i <= b; i++) res.push_back(p1[i]);
for (int i = a; i < p2.size(); i++) res.push_back(p2[i]);
for (int i = 0; i < a; i++) res.push_back(p2[i]);
return res;
}
else {
if (p1.size() > p2.size()) return p1;
return p2;
}
return {}; // never reached
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:68:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i = 0; i < p1.size(); i++) res.push_back(p1[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:69:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i = 0; i < p2.size(); i++) res.push_back(p2[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:75:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int i = 0; i < p1.size(); i++) res.push_back(p1[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:76:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int i = 0; i < p2.size(); i++) res.push_back(p2[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:80:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int i = 0; i < p1.size(); i++) res.push_back(p1[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:81:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int i = 0; i < p2.size(); i++) res.push_back(p2[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:86:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int i = 0; i < p1.size(); i++) res.push_back(p1[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:87:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = 0; i < p2.size(); i++) res.push_back(p2[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:93:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int i = b + 1; i < p1.size(); i++) res.push_back(p1[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:95:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for (int i = a; i < p2.size(); i++) res.push_back(p2[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:37:9: warning: variable 'ch' set but not used [-Wunused-but-set-variable]
37 | int ch[256]; for (int i = 0; i < n; i++) ch[i] = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
344 KB |
Output is correct |
4 |
Correct |
8 ms |
436 KB |
Output is correct |
5 |
Correct |
6 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 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 |
8 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
500 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
8 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
8 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |