#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x << ' '; print(x); cerr << endl;
#else
#define dbg(x)
#endif
void print(int x) {cerr << x;}
void print(long long x) {cerr << x;}
void print(char x) {cerr << x;}
void print(string x) {cerr << x;}
void print(double x) {cerr << x;}
template <class T> void print(vector <T> x);
template <class T> void print(set <T> x);
template <class T> void print(multiset <T> x);
template <class T, class V> void print(pair <T, V> x);
template <class T, class V> void print(map <T, V> x);
template <class T> void print(vector <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T> void print(set <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T> void print(multiset <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T, class V> void print(pair <T, V> x) {cerr << "{"; print(x.first); cerr << ' '; print(x.second); cerr << "}";}
template <class T, class V> void print(map <T, V> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
#define ll long long
#define pb push_back
#define ppb pop_back
#define PII pair <int, int>
#define PLL pair <ll, ll>
#define all(v) (v).begin(), (v).end()
#define OK cerr << "OK\n";
#define MP make_pair
vector <int> operator+(vector <int> x, vector <int> y){
for(int i : y) x.pb(i);
return x;
}
vector <int> longest_trip(int N, int D){
vector <int> a, b;
a.pb(0);
b.pb(1);
for(int i = 2; i < N; i++){
if(are_connected({a.back()}, {i})){
a.pb(i);
continue;
}
if(are_connected({b.back()}, {i})){
b.pb(i);
continue;
}
reverse(all(b));
a = a + b;
b.clear();
b.pb(i);
}
int lenA = a.size(), lenB = b.size();
set <int> to_check;
for(int i : a) to_check.insert(i);
for(int i : b) to_check.insert(i);
assert(to_check.size() == lenA + lenB);
if(!are_connected(a, b)){
if(lenA > lenB) return a;
else return b;
}
if(are_connected({a.back()}, {b.back()})){
reverse(all(b));
a = a + b;
return a;
}
if(are_connected({a.back()}, {b.front()})){
a = a + b;
return a;
}
if(are_connected({a.front()}, {b.back()})){
b = b + a;
return b;
}
if(are_connected({a.front()}, {b.front()})){
reverse(all(a));
a = a + b;
return a;
}
vector <int> A, B;
int bottom = 0, top = lenA, resA, resB;
B = b;
while(bottom <= top){
int mid = (top + bottom) / 2;
A.clear();
for(int i = bottom; i <= mid; i++){
A.pb(i);
}
if(are_connected(A, B)){
resA = mid;
top = mid - 1;
}
else{
bottom = mid + 1;
}
}
bottom = 0, top = lenB;
while(bottom <= top){
int mid = (top + bottom) / 2;
B.clear();
for(int i = bottom; i <= mid; i++){
B.pb(i);
}
if(are_connected({resA}, B)){
resB = mid;
top = mid - 1;
}
else{
bottom = mid + 1;
}
}
A.clear(), B.clear();
for(int i = resA - 1; i >= 0; i--) A.pb(a[i]);
for(int i = lenA - 1; i >= resA; i--) A.pb(a[i]);
for(int i = resB; i < lenB; i++) B.pb(b[i]);
for(int i = 0; i < resB; i++) B.pb(b[i]);
A = A + b;
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... |