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 <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
int bsmin(vector<int> A, vector<int> B) {
int l = 0, r = A.size();
while(l < r) {
int m = l + (r - l) / 2;
vector<int> C;
for(int i = l; i <= m; i++) C.push_back(A[i]);
if(are_connected(C, B)) {
r = m;
} else {
l = m + 1;
}
}
return l;
}
vector<int> longest_trip(int N, int D) {
vector<int> o(N);
for(int i = 0; i < N; i++) o[i] = i;
int rng = N - 1;
while(rng % 2 == 0) rng /= 2;
for(int i = 0; i < N; i++) swap(o[i], o[(i * i * rng) % N]);
vector<int> A, B;
A.push_back(o[0]);
bool not_con = false;
for(int i = 1; i < N; i++) {
if(are_connected({A.back()}, {o[i]})) {
A.push_back(o[i]);
not_con = false;
} else {
if(B.size() == 0 || not_con) {
B.push_back(o[i]);
not_con = true;
} else {
if(are_connected({B.back()}, {o[i]})) {
B.push_back(o[i]);
not_con = true;
} else {
while(B.size() > 0) {
A.push_back(B.back());
B.pop_back();
}
B.push_back(o[i]);
not_con = false;
}
}
}
}
if(A.size() < B.size()) swap(A, B);
if(B.empty()) return A;
if(are_connected({A[0]}, {B[0]})) reverse(A.begin(), A.end());
if(are_connected({A.back()}, {B.back()})) reverse(B.begin(), B.end());
if(are_connected({A[0]}, {B.back()})) reverse(A.begin(), A.end()), reverse(B.begin(), B.end());
if(are_connected({A.back()}, {B[0]})) {
for(int i : B) A.push_back(i);
return A;
}
if(are_connected(A, B)) {
int j = bsmin(A, B);
int k = bsmin(B, {A[j]});
rotate(A.begin(), A.begin() + (j + 1) % A.size(), A.end());
rotate(B.begin(), B.begin() + k, B.end());
for(int i : B) A.push_back(i);
return A;
}
return A;
}
# | 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... |