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;
vector<int> longest_trip(int N, int D)
{
vector<int> p1 = {0}, p2 = {1};
for (int i = 2; i < N; i++)
{
if (are_connected({p1.back()}, {i})) p1.push_back(i);
else if (are_connected({p2.back()}, {i})) p2.push_back(i);
else
{
while (!p2.empty())
{
p1.push_back(p2.back());
p2.pop_back();
}
p2 = {i};
}
}
if (p1.size() < p2.size()) swap(p1, p2);
if (!are_connected(p1, p2)) return p1;
if (are_connected({p1.back()}, {p2.back()}))
{
while (!p2.empty())
{
p1.push_back(p2.back());
p2.pop_back();
}
}
else if (are_connected({p1[0]}, {p2.back()}))
{
for (int &i : p1) p2.push_back(i);
p1 = {};
swap(p1, p2);
}
else if (are_connected({p1.back()}, {p2[0]}))
{
for (int &i : p2) p1.push_back(i);
p2 = {};
}
else if (are_connected({p1[0]}, {p2[0]}))
{
reverse(p1.begin(), p1.end());
for (int &i : p2) p1.push_back(i);
p2 = {};
}
else
{
{
int l = 0, r = (int)p1.size() - 1;
while (l < r)
{
int mid = (l + r) >> 1;
vector<int> q;
for (int i = l; i <= mid; i++) q.push_back(p1[i]);
if (are_connected(q, p2)) r = mid;
else l = mid + 1;
}
vector<int> nw = {p1[l]};
for (int i = (l + 1) % (int)p1.size(); i != l; i = (i + 1) % (int)p1.size())
{
nw.push_back(p1[i]);
}
swap(nw, p1);
}
{
int l = 0, r = (int)p2.size() - 1;
while (l < r)
{
int mid = (l + r) >> 1;
vector<int> q;
for (int i = l; i <= mid; i++) q.push_back(p2[i]);
if (are_connected(q, {p1[0]})) r = mid;
else l = mid + 1;
}
vector<int> nw = {p2[l]};
for (int i = (l + 1) % (int)p2.size(); i != l; i = (i + 1) % (int)p2.size())
{
nw.push_back(p2[i]);
}
swap(nw, p2);
}
reverse(p1.begin(), p1.end());
for (int &i : p2) p1.push_back(i);
p2 = {};
}
return p1;
}
# | 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... |