#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 (p1.size() == 1 || are_connected({p1.front()}, {p1.back()})) {
if (p2.size() == 1 || are_connected({p2.front()}, {p2.back()})) {
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;
}
}
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;
}
}
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:70:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int i = b + 1; i < p1.size(); i++) res.push_back(p1[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:72:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = a; i < p2.size(); i++) res.push_back(p2[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:79:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 0; i < p1.size(); i++) res.push_back(p1[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 < 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:91:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for (int i = 0; i < p1.size(); i++) res.push_back(p1[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:92:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int i = 0; i < p2.size(); i++) res.push_back(p2[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:97:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int i = 0; i < p1.size(); i++) res.push_back(p1[i]);
| ~~^~~~~~~~~~~
longesttrip.cpp:98:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (int i = 0; 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;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 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 |
5 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 |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
10 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 |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
600 KB |
Output is correct |
12 |
Correct |
5 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
344 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 |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
344 KB |
Output is correct |
6 |
Correct |
14 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 |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
4 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 |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
436 KB |
Output is correct |
20 |
Correct |
5 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
340 KB |
Output is correct |
23 |
Correct |
6 ms |
440 KB |
Output is correct |
24 |
Correct |
5 ms |
600 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
7 ms |
344 KB |
Output is correct |
31 |
Correct |
8 ms |
596 KB |
Output is correct |
32 |
Correct |
6 ms |
344 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
7 ms |
344 KB |
Output is correct |
36 |
Correct |
5 ms |
600 KB |
Output is correct |
37 |
Correct |
6 ms |
344 KB |
Output is correct |
38 |
Correct |
8 ms |
344 KB |
Output is correct |
39 |
Correct |
9 ms |
436 KB |
Output is correct |
40 |
Correct |
7 ms |
344 KB |
Output is correct |
41 |
Correct |
7 ms |
852 KB |
Output is correct |
42 |
Correct |
8 ms |
344 KB |
Output is correct |
43 |
Correct |
7 ms |
600 KB |
Output is correct |
44 |
Correct |
8 ms |
600 KB |
Output is correct |
45 |
Correct |
9 ms |
344 KB |
Output is correct |
46 |
Correct |
13 ms |
344 KB |
Output is correct |
47 |
Correct |
9 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
344 KB |
Output is correct |
49 |
Correct |
8 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
432 KB |
Output is correct |
51 |
Correct |
8 ms |
436 KB |
Output is correct |
52 |
Correct |
9 ms |
344 KB |
Output is correct |
53 |
Correct |
6 ms |
344 KB |
Output is correct |
54 |
Correct |
7 ms |
344 KB |
Output is correct |
55 |
Correct |
7 ms |
344 KB |
Output is correct |
56 |
Correct |
5 ms |
436 KB |
Output is correct |
57 |
Correct |
7 ms |
436 KB |
Output is correct |
58 |
Correct |
7 ms |
600 KB |
Output is correct |
59 |
Correct |
7 ms |
600 KB |
Output is correct |
60 |
Correct |
8 ms |
604 KB |
Output is correct |
61 |
Correct |
7 ms |
344 KB |
Output is correct |
62 |
Correct |
7 ms |
444 KB |
Output is correct |
63 |
Correct |
8 ms |
604 KB |
Output is correct |
64 |
Correct |
11 ms |
856 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 |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
11 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 |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
436 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
5 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
8 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 |
7 ms |
344 KB |
Output is correct |
25 |
Correct |
6 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
6 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 |
9 ms |
344 KB |
Output is correct |
33 |
Correct |
10 ms |
344 KB |
Output is correct |
34 |
Correct |
8 ms |
344 KB |
Output is correct |
35 |
Correct |
9 ms |
344 KB |
Output is correct |
36 |
Correct |
7 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
432 KB |
Output is correct |
38 |
Correct |
7 ms |
432 KB |
Output is correct |
39 |
Correct |
7 ms |
344 KB |
Output is correct |
40 |
Correct |
6 ms |
344 KB |
Output is correct |
41 |
Correct |
7 ms |
344 KB |
Output is correct |
42 |
Correct |
7 ms |
344 KB |
Output is correct |
43 |
Correct |
6 ms |
600 KB |
Output is correct |
44 |
Correct |
6 ms |
344 KB |
Output is correct |
45 |
Correct |
6 ms |
436 KB |
Output is correct |
46 |
Correct |
6 ms |
604 KB |
Output is correct |
47 |
Correct |
7 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
600 KB |
Output is correct |
49 |
Correct |
8 ms |
344 KB |
Output is correct |
50 |
Correct |
9 ms |
344 KB |
Output is correct |
51 |
Correct |
7 ms |
600 KB |
Output is correct |
52 |
Correct |
6 ms |
344 KB |
Output is correct |
53 |
Correct |
7 ms |
344 KB |
Output is correct |
54 |
Correct |
6 ms |
344 KB |
Output is correct |
55 |
Correct |
6 ms |
344 KB |
Output is correct |
56 |
Correct |
6 ms |
600 KB |
Output is correct |
57 |
Correct |
7 ms |
600 KB |
Output is correct |
58 |
Correct |
7 ms |
600 KB |
Output is correct |
59 |
Correct |
8 ms |
344 KB |
Output is correct |
60 |
Correct |
7 ms |
596 KB |
Output is correct |
61 |
Correct |
7 ms |
344 KB |
Output is correct |
62 |
Correct |
7 ms |
688 KB |
Output is correct |
63 |
Correct |
7 ms |
600 KB |
Output is correct |
64 |
Partially correct |
9 ms |
856 KB |
Output is partially correct |
65 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
66 |
Partially correct |
7 ms |
440 KB |
Output is partially correct |
67 |
Partially correct |
9 ms |
344 KB |
Output is partially correct |
68 |
Partially correct |
7 ms |
592 KB |
Output is partially correct |
69 |
Partially correct |
8 ms |
440 KB |
Output is partially correct |
70 |
Partially correct |
8 ms |
440 KB |
Output is partially correct |
71 |
Correct |
7 ms |
444 KB |
Output is correct |
72 |
Correct |
8 ms |
600 KB |
Output is correct |
73 |
Partially correct |
8 ms |
440 KB |
Output is partially correct |
74 |
Partially correct |
7 ms |
856 KB |
Output is partially correct |
75 |
Partially correct |
8 ms |
608 KB |
Output is partially correct |
76 |
Correct |
8 ms |
344 KB |
Output is correct |
77 |
Correct |
7 ms |
600 KB |
Output is correct |
78 |
Partially correct |
8 ms |
432 KB |
Output is partially correct |
79 |
Partially correct |
8 ms |
688 KB |
Output is partially correct |
80 |
Partially correct |
7 ms |
604 KB |
Output is partially correct |
81 |
Partially correct |
7 ms |
344 KB |
Output is partially correct |
82 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |