#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)
{
mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
n = N;
d = D;
if (D >= 2) {
return solve2();
}
vector<int> vec(n);
for (int i = 0; i < n; i++) {
vec[i] = i;
}
for (int i = 0; i < n; i++) {
swap(vec[i], vec[gen() % (i + 1)]);
}
deque<int> dq1, dq2;
dq1.push_back(vec[0]);
for (int i = 1; i < n; i++) {
if (are_connected({dq1[dq1.size() - 1]}, {vec[i]})) {
dq1.push_back(vec[i]);
}
else if (dq2.size() == 0 || are_connected({dq2[dq2.size() - 1]}, {vec[i]})) {
dq2.push_back(vec[i]);
}
else {
while (!dq2.empty()) {
dq1.push_back(dq2[dq2.size() - 1]);
dq2.pop_back();
}
dq2.push_back(vec[i]);
}
if (gen() % 4 == 0) {
swap(dq1, dq2);
}
}
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
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:161:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
161 | for (int i = 0; i < ans1.size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:164:23: 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 < ans2.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Runtime error |
0 ms |
344 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
436 KB |
Output is correct |
5 |
Correct |
6 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
6 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 |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
4 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 |
4 ms |
344 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 |
Incorrect |
1 ms |
344 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |