#include "longesttrip.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <random>
#include <vector>
typedef long long llong;
const int MAXN = 256 + 10;
const llong INF = 1e18;
const int INTINF = 1e9;
int n;
std::mt19937 rng(420);
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)
{
if (b.size() || (rng() & 1))
{
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);
}
}
}
} else
{
bool resB = are_connected({b.back()}, {i});
if (resB)
{
b.push_back(i);
} else
{
bool resA = are_connected({a.back()}, {i});
if (resA)
{
a.push_back(i);
} else
{
while (!a.empty())
{
b.push_back(a.back());
a.pop_back();
}
b.push_back(i);
a = b;
b.clear();
}
}
}
}
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 (b.size() > 1 && 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 + 1);
if (!are_connected(prefix, b)) l = mid;
else r = mid;
}
aPos = r;
std::vector <int> prefixA(a.begin(), a.begin() + aPos + 1);
l = -1; r = b.size() - 1;
while (l < r - 1)
{
mid = (l + r) / 2;
std::vector <int> prefixB(b.begin(), b.begin() + mid + 1);
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:99:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
longesttrip.cpp:111:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
longesttrip.cpp:121:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
longesttrip.cpp:164:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
164 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |