#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, m, q, k, x, y, a, b, c, d;
vector<int> solve2() {
deque<int> dq;
x = -1;
y = -1;
if (are_connected({0}, {1})) {
x = 0;
y = 1;
}
else if (are_connected({0}, {2})) {
x = 0;
y = 2;
}
else {
x = 1;
y = 2;
}
dq.push_back(x);
dq.push_back(y);
for (int i = 0; i < n; i++) {
if (i == x || i == y) {
continue;
}
if (are_connected({dq[0]}, {i})) {
dq.push_front(i);
}
else if (d >= 2 || are_connected({dq[dq.size() - 1]}, {i})) {
dq.push_back(i);
}
}
vector<int> ans;
for (auto i : dq) {
ans.push_back(i);
}
return ans;
}
vector<int> longest_trip(int N, int D)
{
n = N;
d = D;
if (D >= 2) {
return solve2();
}
deque<int> dq1, dq2;
dq1.push_back(0);
for (int i = 1; i < n; i++) {
if (are_connected({dq1[dq1.size() - 1]}, {i})) {
dq1.push_back(i);
}
else if (dq2.size() == 0 || are_connected({dq2[dq2.size() - 1]}, {i})) {
dq2.push_back(i);
}
else {
while (!dq2.empty()) {
dq1.push_back(dq2[dq2.size() - 1]);
dq2.pop_back();
}
dq2.push_back(i);
}
}
vector<int> ans;
vector<int> ans1, ans2;
for (auto i : dq1) {
ans1.push_back(i);
}
for (auto i : dq2) {
ans2.push_back(i);
}
if (ans2.size() == 0 || !are_connected(ans1, ans2)) {
if (dq1.size() > dq2.size()) {
for (auto i : dq1) {
ans.push_back(i);
}
}
else {
for (auto i : dq2) {
ans.push_back(i);
}
}
return ans;
}
if (are_connected({ans1[0]}, {ans2[0]})) {
reverse(ans1.begin(), ans1.end());
for (int i : ans2) {
ans1.push_back(i);
}
return ans1;
}
if (are_connected({ans1[0]}, {ans2[n - 1]})) {
reverse(ans1.begin(), ans1.end());
reverse(ans2.begin(), ans2.end());
for (int i : ans2) {
ans1.push_back(i);
}
return ans1;
}
if (are_connected({ans1[n - 1]}, {ans2[0]})) {
for (int i : ans2) {
ans1.push_back(i);
}
return ans1;
}
if (are_connected({ans1[n - 1]}, {ans2[n - 1]})) {
reverse(ans2.begin(), ans2.end());
for (int i : ans2) {
ans1.push_back(i);
}
return ans1;
}
ll l = 0, r = ans1.size() - 1;
while (r > l) {
ll mid = (r + l) / 2;
vector<int> chk;
for (int i = l; i <= mid; i++) {
chk.push_back(ans1[i]);
}
if (are_connected(chk, ans2)) {
r = mid;
}
else {
l = mid + 1;
}
}
ll e = l;
l = 0;
r = ans2.size() - 1;
while (r > l) {
ll mid = (r + l) / 2;
vector<int> chk;
for (int i = l; i <= mid; i++) {
chk.push_back(ans2[i]);
}
if (are_connected({ans1[e]}, chk)) {
r = mid;
}
else {
l = mid + 1;
}
}
ll f = l;
for (int i = 0; i < ans1.size(); i++) {
ans.push_back(ans1[(e + 1 + i) % ans1.size()]);
}
for (int i = 0; i < ans2.size(); i++) {
ans.push_back(ans2[(f + i) % ans2.size()]);
}
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:149:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
149 | for (int i = 0; i < ans1.size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:152:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
152 | for (int i = 0; i < ans2.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
420 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
596 KB |
Output is correct |
8 |
Correct |
7 ms |
348 KB |
Output is correct |
9 |
Correct |
8 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
432 KB |
Output is correct |
11 |
Correct |
7 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Correct |
6 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 |
436 KB |
Output is correct |
4 |
Correct |
7 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
non-disjoint arrays |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
344 KB |
non-disjoint arrays |
7 |
Halted |
0 ms |
0 KB |
- |