# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
840504 | ogkostya | Longest Trip (IOI23_longesttrip) | C++17 | 21 ms | 332 KiB |
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 <algorithm>
#include <random>
std::vector<int> longest_trip(int N, int D)
{
std::vector<int> pos = {};
for (int i = 0; i < N; i++)
{
pos.push_back(i);
}
// First create an instance of an engine.
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(begin(pos), end(pos), g);
std::vector<int> an1 = {};
std::vector<int> an2 = {};
an1.push_back(pos[0]);
std::vector<int> temp1 = {};
std::vector<int> temp2 = {};
int size = pos.size();
bool endnoconnect = false;
for (int ii = 1; ii < size; ii++)
{
int i = pos[ii];
temp1.clear();
temp1.push_back(i);
temp2.clear();
temp2.push_back(an1.back());
if (are_connected(temp1, temp2))
{
an1.push_back(i);
endnoconnect = false;
}
else
{
if (an2.size() == 0)
{
an2.push_back(i);
endnoconnect = true;
}
else
{
if (endnoconnect)
{
an2.push_back(i);
if (an2.size() > an1.size())
{
std::vector<int> an3 = an1;
an1 = an2;
an2 = an3;
}
endnoconnect = true;
}
else
{
temp2.clear();
temp2.push_back(an2.back());
if (are_connected(temp1, temp2))
{
an2.push_back(i);
if (an2.size() > an1.size())
{
std::vector<int> an3 = an1;
an1 = an2;
an2 = an3;
}
endnoconnect = true;
}
else
{
while (an2.size() > 0)
{
an1.push_back(an2.back());
an2.pop_back();
}
an2.push_back(i);
endnoconnect = false;
}
}
}
}
}
if (an2.size() > 0)
{
if (an2.size() > an1.size())
{
std::vector<int> an3 = an1;
an1 = an2;
an2 = an3;
}
temp1.clear();
temp1.push_back(an1.back());
temp2.clear();
temp2.push_back(an2.back());
if (are_connected(temp1, temp2))
{
while (an2.size() > 0)
{
an1.push_back(an2.back());
an2.pop_back();
}
}
else
{
temp2.clear();
temp2.push_back(an2.front());
if (are_connected(temp1, temp2))
{
int l = an2.size();
for (int i = 0; i < l; i++)
{
an1.push_back(an2[i]);
}
}
else
{
temp1.clear();
temp1.push_back(an1.front());
temp2.clear();
temp2.push_back(an2.back());
if (are_connected(temp1, temp2))
{
int l = an1.size();
for (int i = 0; i < l; i++)
{
an2.push_back(an1[i]);
}
an1 = an2;
}
else
{
if (are_connected(an1, an2))
{
int l = 0, r = an1.size() - 1;
while (l < r)
{
int m = (l + r) / 2;
temp1.clear();
for (int i = l; i <= m; i++)
{
temp1.push_back(an1[i]);
}
if (are_connected(temp1, an2))
{
r = m;
}
else
{
l = m + 1;
}
}
int c1 = l;
temp1.clear();
temp1.push_back(an1[c1]);
l = 0, r = an2.size() - 1;
while (l < r)
{
int m = (l + r) / 2;
temp2.clear();
for (int i = l; i <= m; i++)
{
temp2.push_back(an2[i]);
}
if (are_connected(temp1, temp2))
{
r = m;
}
else
{
l = m + 1;
}
}
int c2 = l;
std::vector<int> an3 = {};
int l1 = an1.size();
int l2 = an2.size();
for (int i = c1+1; i < l1; i++)
{
an3.push_back(an1[i]);
}
for (int i = 0; i <= c1; i++)
{
an3.push_back(an1[i]);
}
for (int i = c2; i < l2; i++)
{
an3.push_back(an2[i]);
}
for (int i = 0; i < c2; i++)
{
an3.push_back(an2[i]);
}
an1 = an3;
}
}
}
}
}
return an1;
}
# | 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... |