#include "longesttrip.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 256 + 10;
const llong INF = 1e18;
const int INTINF = 1e9;
int n;
std::vector <int> longest_trip(int N, int D)
{
n = N;
std::vector <int> a, b;
a.push_back(0);
for (int i = 1 ; i < n ; ++i)
{
bool resA = are_connected({a.back()}, {i});
if (resA)
{
a.push_back(i);
} else
{
if (b.empty())
{
b.push_back(i);
} else
{
bool resB = are_connected({b.back()}, {i});
if (resB)
{
b.push_back(i);
} else
{
while (!b.empty())
{
a.push_back(b.back());
b.pop_back();
}
b.push_back(i);
}
}
}
}
if (a.size() < b.size())
{
std::swap(a, b);
}
if (b.empty())
{
return a;
}
if (!are_connected(a, b))
{
return a;
}
if (are_connected({a[0]}, {b[0]}))
{
std::reverse(a.begin(), a.end());
for (int i = 0 ; i < b.size() ; ++i)
{
a.push_back(b[i]);
}
return a;
}
if (are_connected({a[0]}, {b.back()}))
{
std::reverse(b.begin(), b.end());
std::reverse(a.begin(), a.end());
for (int i = 0 ; i < b.size() ; ++i)
{
a.push_back(b[i]);
}
return a;
}
if (are_connected({b[0]}, {a.back()}))
{
for (int i = 0 ; i < b.size() ; ++i)
{
a.push_back(b[i]);
}
return a;
}
int aPos, bPos;
int l = -1, r = a.size() - 1, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
std::vector <int> prefix(a.begin(), a.begin() + mid);
if (prefix.size() != mid + 1)
{
exit(prefix.size());
}
if (!are_connected(prefix, b)) l = mid;
else r = mid;
}
aPos = r;
std::vector <int> prefixA(a.begin(), a.begin() + aPos);
l = -1; r = b.size() - 1;
while (l < r - 1)
{
mid = (l + r) / 2;
std::vector <int> prefixB(b.begin(), b.begin() + mid);
if (!are_connected(prefixA, prefixB)) l = mid;
else r = mid;
}
bPos = r;
for (int i = 0 ; i < aPos ; ++i)
{
a.push_back(a[0]);
a.erase(a.begin());
}
for (int i = 0 ; i < bPos ; ++i)
{
b.push_back(b[0]);
b.erase(b.begin());
}
std::reverse(a.begin(), a.end());
for (int i = 0 ; i < b.size() ; ++i)
{
a.push_back(b[i]);
}
return a;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:68:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
longesttrip.cpp:80:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
longesttrip.cpp:90:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
longesttrip.cpp:104:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
104 | if (prefix.size() != mid + 1)
| ~~~~~~~~~~~~~~^~~~~~~~~~
longesttrip.cpp:138:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
684 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 |
4 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 |
6 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 |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 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 |
6 ms |
408 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
432 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
9 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Runtime error |
1 ms |
344 KB |
Execution failed because the return code was nonzero |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
596 KB |
Output is correct |
2 |
Correct |
5 ms |
412 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
436 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
436 KB |
Output is correct |
14 |
Correct |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Runtime error |
2 ms |
340 KB |
Execution failed because the return code was nonzero |
17 |
Halted |
0 ms |
0 KB |
- |