This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
// cout << "DEBUG" << endl;
if (are_connected({ans1[0]}, {ans2[ans2.size() - 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[ans1.size() - 1]}, {ans2[0]})) {
for (int i : ans2) {
ans1.push_back(i);
}
return ans1;
}
if (are_connected({ans1[ans1.size() - 1]}, {ans2[ans2.size() - 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 (stderr)
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:150:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | for (int i = 0; i < ans1.size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:153:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
153 | for (int i = 0; i < ans2.size(); i++) {
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |