#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
vector<int> solve3(int N) {
vector<int> ans(N);
iota(ans.begin(), ans.end(), 0);
return ans;
}
vector<int> solve2(int N) {
queue<int> q;
vector<int> ans;
for (int i = 0; i < N; i++) {
q.push(i);
}
int extra = -1;
while (!q.empty()) {
int u = q.front();
q.pop();
ans.emplace_back(u);
if (q.empty()) continue;
int v = q.front();
if (!are_connected({u}, {v})) {
q.pop();
if (q.empty()) {
extra = v;
} else {
q.push(v);
}
}
}
vector<int> res;
for (int i = 0; i < ans.size(); i++) {
if (i == 0 && extra != -1) res.emplace_back(extra);
res.emplace_back(ans[i]);
}
for (int i = 1; i < res.size(); i++) {
assert(are_connected({res[i-1]}, {res[i]}));
}
assert(res.size() == N);
return res;
}
vector<int> merge_vectors(const vector<int> &a, const vector<int> &b) {
vector<int> res;
for (int i : a) res.emplace_back(i);
for (int i : b) res.emplace_back(i);
return res;
}
vector<int> get_interval(const vector<int> &a, int l, int r) {
vector<int> res;
for (int i = l; i <= r; i++) {
res.emplace_back(a[i]);
}
return res;
}
int find_connection(const vector<int> &a, const vector<int> &b) {
int lo = 0, hi = a.size() - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (are_connected(get_interval(a, lo, mid), b)) {
hi = mid;
} else {
lo = mid + 1;
}
}
return lo;
}
vector<int> add_to_component(vector<int> comp, int u) {
if (comp.empty()) return {u};
if (comp.size() == 1) return {comp.front(), u};
// TODO remove this and only check with binary search
if (are_connected({comp.back()}, {u})) {
return merge_vectors(comp, {u});
}
int idx = find_connection(comp, {u});
if (idx == 0) {
return merge_vectors({u}, comp);
}
vector<int> res1 = {u, comp[idx]};
auto res2 = merge_vectors(res1, get_interval(comp, idx+1, comp.size()-1));
auto res3 = merge_vectors(res2, get_interval(comp, 0, idx-1));
return res3;
}
vector<int> solve1(int N) {
vector<int> a = {0}, b;
for (int i = 1; i < N; i++) {
if (are_connected(a, {i})) {
a = add_to_component(a, i);
} else {
b = add_to_component(b, i);
}
}
if (b.size() > 0 && are_connected(a, b)) {
if (are_connected({a.front()}, {b.front()})) {
reverse(a.begin(), a.end());
return merge_vectors(a, b);
}
if (are_connected({a.back()}, {b.front()})) {
return merge_vectors(a, b);
}
if (are_connected({a.front()}, {b.back()})) {
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
return merge_vectors(a, b);
}
if (are_connected({a.back()}, {b.back()})) {
reverse(b.begin(), b.end());
return merge_vectors(a, b);
}
int idx_in_a = find_connection(a, b);
int idx_in_b = find_connection(b, a);
auto res1 = get_interval(a, idx_in_a+1, a.size()-1);
auto res2 = merge_vectors(res1, get_interval(a, 0, idx_in_a));
auto res3 = merge_vectors(res2, get_interval(b, idx_in_b, b.size()-1));
auto res4 = merge_vectors(res3, get_interval(b, 0, idx_in_b-1));
return res4;
} else {
if (a.size() > b.size()) return a;
else return b;
}
}
vector<int> longest_trip(int N, int D)
{
if (D == 3) return solve3(N);
if (D == 2) return solve2(N);
if (D == 1) return solve1(N);
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> solve2(int)':
longesttrip.cpp:36:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int i = 0; i < ans.size(); i++) {
| ~~^~~~~~~~~~~~
longesttrip.cpp:40:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 1; i < res.size(); i++) {
| ~~^~~~~~~~~~~~
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:1:
longesttrip.cpp:43:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
43 | assert(res.size() == N);
| ~~~~~~~~~~~^~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:146:1: warning: control reaches end of non-void function [-Wreturn-type]
146 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
344 KB |
Output is correct |
3 |
Correct |
10 ms |
344 KB |
Output is correct |
4 |
Correct |
8 ms |
344 KB |
Output is correct |
5 |
Correct |
10 ms |
344 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
8 ms |
344 KB |
Output is correct |
9 |
Correct |
8 ms |
600 KB |
Output is correct |
10 |
Correct |
8 ms |
344 KB |
Output is correct |
11 |
Correct |
10 ms |
344 KB |
Output is correct |
12 |
Correct |
9 ms |
344 KB |
Output is correct |
13 |
Correct |
10 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
11 ms |
344 KB |
Output is correct |
4 |
Correct |
13 ms |
444 KB |
Output is correct |
5 |
Correct |
21 ms |
1224 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
11 ms |
344 KB |
Output is correct |
9 |
Correct |
13 ms |
436 KB |
Output is correct |
10 |
Correct |
20 ms |
472 KB |
Output is correct |
11 |
Correct |
21 ms |
460 KB |
Output is correct |
12 |
Correct |
21 ms |
860 KB |
Output is correct |
13 |
Correct |
18 ms |
712 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Correct |
17 ms |
344 KB |
Output is correct |
17 |
Correct |
13 ms |
344 KB |
Output is correct |
18 |
Correct |
15 ms |
344 KB |
Output is correct |
19 |
Correct |
19 ms |
600 KB |
Output is correct |
20 |
Correct |
19 ms |
440 KB |
Output is correct |
21 |
Correct |
24 ms |
696 KB |
Output is correct |
22 |
Correct |
22 ms |
1228 KB |
Output is correct |
23 |
Correct |
24 ms |
724 KB |
Output is correct |
24 |
Correct |
28 ms |
440 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
344 KB |
Output is correct |
27 |
Correct |
11 ms |
344 KB |
Output is correct |
28 |
Correct |
8 ms |
344 KB |
Output is correct |
29 |
Correct |
9 ms |
344 KB |
Output is correct |
30 |
Correct |
13 ms |
432 KB |
Output is correct |
31 |
Correct |
12 ms |
684 KB |
Output is correct |
32 |
Correct |
9 ms |
344 KB |
Output is correct |
33 |
Correct |
11 ms |
440 KB |
Output is correct |
34 |
Correct |
12 ms |
436 KB |
Output is correct |
35 |
Correct |
11 ms |
440 KB |
Output is correct |
36 |
Correct |
19 ms |
716 KB |
Output is correct |
37 |
Correct |
21 ms |
724 KB |
Output is correct |
38 |
Correct |
18 ms |
708 KB |
Output is correct |
39 |
Correct |
15 ms |
700 KB |
Output is correct |
40 |
Correct |
13 ms |
444 KB |
Output is correct |
41 |
Correct |
15 ms |
944 KB |
Output is correct |
42 |
Correct |
14 ms |
696 KB |
Output is correct |
43 |
Correct |
14 ms |
440 KB |
Output is correct |
44 |
Correct |
17 ms |
704 KB |
Output is correct |
45 |
Correct |
11 ms |
344 KB |
Output is correct |
46 |
Correct |
10 ms |
344 KB |
Output is correct |
47 |
Correct |
10 ms |
344 KB |
Output is correct |
48 |
Correct |
13 ms |
344 KB |
Output is correct |
49 |
Correct |
11 ms |
344 KB |
Output is correct |
50 |
Correct |
11 ms |
432 KB |
Output is correct |
51 |
Correct |
15 ms |
436 KB |
Output is correct |
52 |
Correct |
13 ms |
436 KB |
Output is correct |
53 |
Correct |
14 ms |
600 KB |
Output is correct |
54 |
Correct |
14 ms |
604 KB |
Output is correct |
55 |
Correct |
13 ms |
600 KB |
Output is correct |
56 |
Correct |
20 ms |
700 KB |
Output is correct |
57 |
Correct |
20 ms |
976 KB |
Output is correct |
58 |
Correct |
23 ms |
436 KB |
Output is correct |
59 |
Correct |
22 ms |
708 KB |
Output is correct |
60 |
Correct |
16 ms |
700 KB |
Output is correct |
61 |
Correct |
27 ms |
708 KB |
Output is correct |
62 |
Correct |
19 ms |
952 KB |
Output is correct |
63 |
Correct |
18 ms |
692 KB |
Output is correct |
64 |
Correct |
25 ms |
688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
10 ms |
344 KB |
Output is correct |
3 |
Correct |
11 ms |
600 KB |
Output is correct |
4 |
Correct |
13 ms |
448 KB |
Output is correct |
5 |
Partially correct |
21 ms |
716 KB |
Output is partially correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
11 ms |
344 KB |
Output is correct |
8 |
Correct |
11 ms |
344 KB |
Output is correct |
9 |
Correct |
12 ms |
440 KB |
Output is correct |
10 |
Partially correct |
18 ms |
468 KB |
Output is partially correct |
11 |
Partially correct |
21 ms |
464 KB |
Output is partially correct |
12 |
Partially correct |
18 ms |
720 KB |
Output is partially correct |
13 |
Partially correct |
20 ms |
716 KB |
Output is partially correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Correct |
14 ms |
344 KB |
Output is correct |
17 |
Correct |
14 ms |
344 KB |
Output is correct |
18 |
Correct |
15 ms |
344 KB |
Output is correct |
19 |
Correct |
19 ms |
436 KB |
Output is correct |
20 |
Correct |
18 ms |
440 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
6 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 |
10 ms |
344 KB |
Output is correct |
26 |
Correct |
11 ms |
432 KB |
Output is correct |
27 |
Correct |
10 ms |
432 KB |
Output is correct |
28 |
Correct |
9 ms |
344 KB |
Output is correct |
29 |
Correct |
16 ms |
436 KB |
Output is correct |
30 |
Correct |
12 ms |
432 KB |
Output is correct |
31 |
Correct |
12 ms |
436 KB |
Output is correct |
32 |
Correct |
9 ms |
344 KB |
Output is correct |
33 |
Correct |
10 ms |
344 KB |
Output is correct |
34 |
Correct |
11 ms |
344 KB |
Output is correct |
35 |
Correct |
13 ms |
344 KB |
Output is correct |
36 |
Correct |
13 ms |
344 KB |
Output is correct |
37 |
Correct |
12 ms |
432 KB |
Output is correct |
38 |
Correct |
14 ms |
436 KB |
Output is correct |
39 |
Correct |
14 ms |
440 KB |
Output is correct |
40 |
Partially correct |
14 ms |
444 KB |
Output is partially correct |
41 |
Correct |
13 ms |
440 KB |
Output is correct |
42 |
Correct |
13 ms |
436 KB |
Output is correct |
43 |
Partially correct |
23 ms |
972 KB |
Output is partially correct |
44 |
Partially correct |
27 ms |
724 KB |
Output is partially correct |
45 |
Partially correct |
29 ms |
720 KB |
Output is partially correct |
46 |
Partially correct |
22 ms |
720 KB |
Output is partially correct |
47 |
Partially correct |
18 ms |
720 KB |
Output is partially correct |
48 |
Partially correct |
20 ms |
468 KB |
Output is partially correct |
49 |
Partially correct |
17 ms |
708 KB |
Output is partially correct |
50 |
Partially correct |
14 ms |
708 KB |
Output is partially correct |
51 |
Partially correct |
18 ms |
452 KB |
Output is partially correct |
52 |
Partially correct |
14 ms |
692 KB |
Output is partially correct |
53 |
Partially correct |
14 ms |
860 KB |
Output is partially correct |
54 |
Partially correct |
16 ms |
700 KB |
Output is partially correct |
55 |
Partially correct |
14 ms |
700 KB |
Output is partially correct |
56 |
Partially correct |
20 ms |
976 KB |
Output is partially correct |
57 |
Partially correct |
17 ms |
460 KB |
Output is partially correct |
58 |
Partially correct |
13 ms |
868 KB |
Output is partially correct |
59 |
Partially correct |
12 ms |
704 KB |
Output is partially correct |
60 |
Partially correct |
14 ms |
696 KB |
Output is partially correct |
61 |
Partially correct |
14 ms |
700 KB |
Output is partially correct |
62 |
Partially correct |
18 ms |
468 KB |
Output is partially correct |
63 |
Partially correct |
17 ms |
712 KB |
Output is partially correct |
64 |
Partially correct |
20 ms |
1212 KB |
Output is partially correct |
65 |
Partially correct |
19 ms |
704 KB |
Output is partially correct |
66 |
Partially correct |
18 ms |
692 KB |
Output is partially correct |
67 |
Partially correct |
35 ms |
696 KB |
Output is partially correct |
68 |
Partially correct |
18 ms |
700 KB |
Output is partially correct |
69 |
Partially correct |
17 ms |
952 KB |
Output is partially correct |
70 |
Partially correct |
23 ms |
860 KB |
Output is partially correct |
71 |
Partially correct |
20 ms |
468 KB |
Output is partially correct |
72 |
Partially correct |
19 ms |
968 KB |
Output is partially correct |
73 |
Partially correct |
24 ms |
964 KB |
Output is partially correct |
74 |
Partially correct |
17 ms |
700 KB |
Output is partially correct |
75 |
Partially correct |
28 ms |
696 KB |
Output is partially correct |
76 |
Partially correct |
19 ms |
696 KB |
Output is partially correct |
77 |
Partially correct |
19 ms |
720 KB |
Output is partially correct |
78 |
Partially correct |
22 ms |
876 KB |
Output is partially correct |
79 |
Partially correct |
20 ms |
716 KB |
Output is partially correct |
80 |
Partially correct |
20 ms |
700 KB |
Output is partially correct |
81 |
Partially correct |
17 ms |
452 KB |
Output is partially correct |
82 |
Partially correct |
16 ms |
712 KB |
Output is partially correct |