#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
//mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double wow = (double) ((ull) rng()) / ((ull)(0-1));
return wow * (r - l) + l;
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
#include "longesttrip.h"
int o_cnt = 0;
bool b_connected(vector<int> a, vector<int> b){
o_cnt++;
return are_connected(a, b);
}
vector<int> longest_trip(int n, int d){
o_cnt = 0;
vector<int> perm(n);
for(int i = 0; i < n; ++i) perm[i] = i;
shuffle(ALL(perm), rng);
vector<int> S1, S2;
S1.push_back(perm[0]);
for(int it = 1; it < n; ++it){
int i = perm[it];
if (S2.size()){
if (it + 1 < n){
int j = perm[it + 1];
it++;
if (are_connected({i}, {j})){
if (are_connected({S1.back()}, {i})){
S1.push_back(i); S1.push_back(j);
}
else{
S2.push_back(i); S2.push_back(j);
}
if (are_connected({S1.back()}, {S2.back()})){
while(S2.size()){
S1.push_back(S2.back());
S2.pop_back();
}
}
}
else{
if (are_connected({S1.back()}, {i}) && are_connected({S2.back()}, {j})){
S1.push_back(i); S2.push_back(j);
}
else{
S2.push_back(i); S1.push_back(j);
}
}
}
else{
vector<int> A = {S1.back()}, B = {S2.back()}, C = {i};
if (b_connected(A, C)) {
S1.push_back(i);
if (b_connected(B, C)) {
while(S2.size()){
S1.push_back(S2.back());
S2.pop_back();
}
}
}
else S2.push_back(i);
}
}
else{
if (b_connected({S1.back()}, {i})) S1.push_back(i);
else S2.push_back(i);
}
}
if (S1.size() < S2.size()) swap(S1, S2);
if (S2.empty()) return S1;
if (!b_connected(S1, S2)) return S1;
if (!b_connected({S1[0]}, {S1.back()})){
if (b_connected({S1.back()}, {S2[0]})){
for(int i: S2) S1.push_back(i);
return S1;
}
else{
reverse(ALL(S1));
for(int i: S2) S1.push_back(i);
return S1;
}
}
if (S2.size() > 1 && !b_connected({S2[0]}, {S2.back()})){
swap(S1, S2);
if (b_connected({S1.back()}, {S2[0]})){
for(int i: S2) S1.push_back(i);
return S1;
}
else{
reverse(ALL(S1));
for(int i: S2) S1.push_back(i);
return S1;
}
}
int l = 0, r = S1.size() - 1;
while(l < r){
int mid = (l + r) >> 1;
vector<int> X;
for(int i = 0; i <= mid; ++i) X.push_back(S1[i]);
if (b_connected(X, S2)) r = mid;
else l = mid + 1;
}
int x = l;
l = 0, r = S2.size() - 1;
while(l < r){
int mid = (l + r) >> 1;
vector<int> X;
for(int i = 0; i <= mid; ++i) X.push_back(S2[i]);
if (b_connected({S1[x]}, X)) r = mid;
else l = mid + 1;
}
int y = l;
vector<int> ans;
for(int i = 0; i < (int) S1.size(); ++i) ans.push_back(S1[(i + x) % S1.size()]);
reverse(ALL(ans));
for(int i = 0; i < (int) S2.size(); ++i) ans.push_back(S2[(i + y) % S2.size()]);
return ans;
}
# | 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... |