#include "longesttrip.h"
// 123
#include <bits/stdc++.h>
using namespace std;
std::vector<int> longest_trip(int N, int D) {
vector<int> a[2];
a[0] = {0};
int i = 1;
for (; i < N; i++) {
if (are_connected({a[0].back()}, {i})) {
a[0].emplace_back(i);
} else {
a[1].emplace_back(i++);
break;
}
}
bool merge = 0;
bool ask = 0;
int last_merge_pos = -1;
for (; i < N; i++) {
int x = a[0].back();
int y = a[1].back();
if (merge) {
assert(a[1].back() == i - 1);
if (are_connected({i - 1}, {i})) {
a[1].emplace_back(i);
} else {
a[0].insert(a[0].begin() + last_merge_pos, i);
}
merge = 0;
} else if (are_connected({i}, {x})) {
a[0].emplace_back(i);
ask = 0;
} else {
if (ask) {
a[1].emplace_back(i);
ask = 0;
} else {
if (are_connected({i}, {y})) {
a[1].emplace_back(i);
ask = 1;
} else {
last_merge_pos = a[0].size();
merge = 1;
while (a[1].size()) a[0].emplace_back(a[1].back()), a[1].pop_back();
a[1].emplace_back(i);
}
}
}
}
if (a[0].size() == N) return a[0];
assert(a[0].size() + a[1].size() == N);
int x = a[0][0], y = a[0].back();
int z = a[1][0], t = a[1].back();
auto join = [&](int p, int q) {
if (p) reverse(a[0].begin(), a[0].end());
if (q) reverse(a[1].begin(), a[1].end());
a[0].insert(a[0].end(), a[1].begin(), a[1].end());
return a[0];
};
if (are_connected({x}, {z})) {
return join(1, 0);
} else if (are_connected({x}, {t})) {
return join(1, 1);
} else if (are_connected({y}, {z})) {
return join(0, 0);
} else if (are_connected({y}, {t})) {
return join(0, 1);
} else {
int l1 = 0, r1 = a[0].size();
int l2 = 0, r2 = a[1].size();
auto ask = [&](int x, int y, int z, int t) {
vector<int> p, q;
for (int i = x; i < y; i++) p.emplace_back(a[0][i]);
for (int i = z; i < t; i++) q.emplace_back(a[1][i]);
if (p.size() && q.size()) return are_connected(p, q);
return false;
};
if (!ask(l1, r1, l2, r2)) {
if (a[0].size() < a[1].size()) swap(a[0], a[1]);
return a[0];
} else {
while (l1 + 1 < r1) {
int m = l1 + r1 >> 1;
if (ask(l1, m, l2, r2)) {
r1 = m;
} else {
l1 = m;
}
}
while (l2 + 1 < r2) {
int m = l2 + r2 >> 1;
if (ask(l1, r1, l2, m)) {
r2 = m;
} else {
l2 = m;
}
}
assert(l1 + 1 == r1 && l2 + 1 == r2);
vector<int> ans;
for (int i = r1; i < a[0].size(); i++) ans.emplace_back(a[0][i]);
for (int i = 0; i < r1; i++) ans.emplace_back(a[0][i]);
for (int i = l2; i < a[1].size(); i++) ans.emplace_back(a[1][i]);
for (int i = 0; i < l2; i++) ans.emplace_back(a[1][i]);
return ans;
}
}
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:53:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
53 | if (a[0].size() == N) return a[0];
| ~~~~~~~~~~~~^~~~
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from longesttrip.cpp:4:
longesttrip.cpp:54:42: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
54 | assert(a[0].size() + a[1].size() == N);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
longesttrip.cpp:86:44: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
86 | int m = l1 + r1 >> 1;
| ~~~^~~~
longesttrip.cpp:94:44: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | int m = l2 + r2 >> 1;
| ~~~^~~~
longesttrip.cpp:103:44: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int i = r1; i < a[0].size(); i++) ans.emplace_back(a[0][i]);
| ~~^~~~~~~~~~~~~
longesttrip.cpp:105:44: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (int i = l2; i < a[1].size(); i++) ans.emplace_back(a[1][i]);
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
596 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
600 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 |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
516 KB |
Output is correct |
11 |
Correct |
4 ms |
600 KB |
Output is correct |
12 |
Correct |
5 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
5 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 |
9 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 |
6 ms |
600 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 |
15 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
7 ms |
344 KB |
Output is correct |
18 |
Correct |
5 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 |
4 ms |
344 KB |
Output is correct |
22 |
Correct |
4 ms |
344 KB |
Output is correct |
23 |
Correct |
5 ms |
344 KB |
Output is correct |
24 |
Correct |
5 ms |
344 KB |
Output is correct |
25 |
Correct |
10 ms |
344 KB |
Output is correct |
26 |
Correct |
10 ms |
344 KB |
Output is correct |
27 |
Correct |
11 ms |
344 KB |
Output is correct |
28 |
Correct |
10 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
5 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 |
5 ms |
344 KB |
Output is correct |
34 |
Correct |
5 ms |
600 KB |
Output is correct |
35 |
Correct |
5 ms |
344 KB |
Output is correct |
36 |
Correct |
5 ms |
344 KB |
Output is correct |
37 |
Correct |
5 ms |
600 KB |
Output is correct |
38 |
Correct |
5 ms |
344 KB |
Output is correct |
39 |
Correct |
6 ms |
852 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
6 ms |
344 KB |
Output is correct |
42 |
Correct |
9 ms |
600 KB |
Output is correct |
43 |
Correct |
6 ms |
600 KB |
Output is correct |
44 |
Correct |
7 ms |
344 KB |
Output is correct |
45 |
Correct |
10 ms |
596 KB |
Output is correct |
46 |
Correct |
13 ms |
344 KB |
Output is correct |
47 |
Correct |
9 ms |
344 KB |
Output is correct |
48 |
Correct |
9 ms |
344 KB |
Output is correct |
49 |
Correct |
8 ms |
344 KB |
Output is correct |
50 |
Correct |
5 ms |
344 KB |
Output is correct |
51 |
Correct |
8 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
5 ms |
344 KB |
Output is correct |
54 |
Correct |
6 ms |
600 KB |
Output is correct |
55 |
Correct |
6 ms |
344 KB |
Output is correct |
56 |
Correct |
5 ms |
344 KB |
Output is correct |
57 |
Correct |
5 ms |
344 KB |
Output is correct |
58 |
Correct |
6 ms |
344 KB |
Output is correct |
59 |
Correct |
7 ms |
444 KB |
Output is correct |
60 |
Correct |
6 ms |
600 KB |
Output is correct |
61 |
Correct |
6 ms |
412 KB |
Output is correct |
62 |
Correct |
7 ms |
600 KB |
Output is correct |
63 |
Correct |
6 ms |
436 KB |
Output is correct |
64 |
Correct |
8 ms |
440 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 |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
600 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
496 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
596 KB |
Output is correct |
14 |
Correct |
14 ms |
344 KB |
Output is correct |
15 |
Correct |
12 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
8 ms |
344 KB |
Output is correct |
18 |
Correct |
5 ms |
596 KB |
Output is correct |
19 |
Correct |
5 ms |
344 KB |
Output is correct |
20 |
Correct |
5 ms |
344 KB |
Output is correct |
21 |
Correct |
13 ms |
344 KB |
Output is correct |
22 |
Correct |
11 ms |
344 KB |
Output is correct |
23 |
Correct |
9 ms |
344 KB |
Output is correct |
24 |
Correct |
8 ms |
344 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
8 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
600 KB |
Output is correct |
30 |
Correct |
5 ms |
600 KB |
Output is correct |
31 |
Correct |
5 ms |
344 KB |
Output is correct |
32 |
Correct |
10 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
596 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
7 ms |
344 KB |
Output is correct |
36 |
Correct |
9 ms |
344 KB |
Output is correct |
37 |
Correct |
5 ms |
344 KB |
Output is correct |
38 |
Correct |
6 ms |
344 KB |
Output is correct |
39 |
Correct |
8 ms |
344 KB |
Output is correct |
40 |
Correct |
5 ms |
344 KB |
Output is correct |
41 |
Correct |
5 ms |
600 KB |
Output is correct |
42 |
Correct |
6 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
344 KB |
Output is correct |
44 |
Correct |
6 ms |
344 KB |
Output is correct |
45 |
Correct |
5 ms |
344 KB |
Output is correct |
46 |
Correct |
5 ms |
344 KB |
Output is correct |
47 |
Correct |
4 ms |
344 KB |
Output is correct |
48 |
Correct |
5 ms |
344 KB |
Output is correct |
49 |
Correct |
5 ms |
344 KB |
Output is correct |
50 |
Correct |
5 ms |
344 KB |
Output is correct |
51 |
Correct |
7 ms |
600 KB |
Output is correct |
52 |
Correct |
8 ms |
344 KB |
Output is correct |
53 |
Correct |
6 ms |
344 KB |
Output is correct |
54 |
Correct |
8 ms |
576 KB |
Output is correct |
55 |
Correct |
7 ms |
344 KB |
Output is correct |
56 |
Correct |
5 ms |
344 KB |
Output is correct |
57 |
Correct |
5 ms |
344 KB |
Output is correct |
58 |
Correct |
6 ms |
600 KB |
Output is correct |
59 |
Correct |
6 ms |
344 KB |
Output is correct |
60 |
Correct |
7 ms |
344 KB |
Output is correct |
61 |
Correct |
5 ms |
344 KB |
Output is correct |
62 |
Correct |
5 ms |
344 KB |
Output is correct |
63 |
Correct |
5 ms |
344 KB |
Output is correct |
64 |
Correct |
7 ms |
600 KB |
Output is correct |
65 |
Correct |
6 ms |
692 KB |
Output is correct |
66 |
Correct |
8 ms |
600 KB |
Output is correct |
67 |
Correct |
7 ms |
688 KB |
Output is correct |
68 |
Correct |
7 ms |
344 KB |
Output is correct |
69 |
Correct |
7 ms |
436 KB |
Output is correct |
70 |
Correct |
7 ms |
604 KB |
Output is correct |
71 |
Correct |
5 ms |
344 KB |
Output is correct |
72 |
Correct |
6 ms |
600 KB |
Output is correct |
73 |
Correct |
6 ms |
688 KB |
Output is correct |
74 |
Correct |
7 ms |
856 KB |
Output is correct |
75 |
Correct |
6 ms |
344 KB |
Output is correct |
76 |
Correct |
6 ms |
344 KB |
Output is correct |
77 |
Correct |
6 ms |
344 KB |
Output is correct |
78 |
Correct |
5 ms |
600 KB |
Output is correct |
79 |
Correct |
7 ms |
436 KB |
Output is correct |
80 |
Correct |
6 ms |
600 KB |
Output is correct |
81 |
Correct |
6 ms |
344 KB |
Output is correct |
82 |
Correct |
6 ms |
344 KB |
Output is correct |