#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> solve1(int N) {
vector<int> a = {0}, b;
for (int i = 1; i < N; i++) {
if (b.empty()) b.emplace_back(i);
else if (are_connected({a.back()}, {i})) {
a.emplace_back(i);
} else if (are_connected({b.front()}, {i})) {
b = merge_vectors({i}, b);
} else {
a = merge_vectors(a, b);
b = vector<int>{i};
}
}
if (b.size() > 0 && are_connected(a, b)) {
if (b.size() == 1) {
return merge_vectors(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:132:1: warning: control reaches end of non-void function [-Wreturn-type]
132 | }
| ^
# |
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 |
3 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
340 KB |
Output is correct |
2 |
Correct |
10 ms |
356 KB |
Output is correct |
3 |
Correct |
9 ms |
344 KB |
Output is correct |
4 |
Correct |
8 ms |
344 KB |
Output is correct |
5 |
Correct |
9 ms |
344 KB |
Output is correct |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Correct |
11 ms |
344 KB |
Output is correct |
8 |
Correct |
9 ms |
344 KB |
Output is correct |
9 |
Correct |
8 ms |
344 KB |
Output is correct |
10 |
Correct |
9 ms |
344 KB |
Output is correct |
11 |
Correct |
9 ms |
344 KB |
Output is correct |
12 |
Correct |
8 ms |
344 KB |
Output is correct |
13 |
Correct |
8 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 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 |
5 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 |
5 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 |
4 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |