#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(15645869);
std::vector<int> longest_trip(int N, int D)
{
vector<int>x, y;
vector<int>p;
for(int i=0;i<N;i++){
p.push_back(i);
}
shuffle(p.begin(),p.end(),rng);
for(int i:p){
if(x.size()==0){
x={i};
continue;
}
if(y.size()==0){
if(!are_connected({i}, x)){
y={i};
continue;
}
if(are_connected({i}, {x[0], x.back()})){
if(are_connected({i}, {x[0]})){
x.insert(x.begin(),i);
continue;
} else{
x.push_back(i);
continue;
}
}
int lo=1;
int hi=x.size()-2;
while(lo<hi){
int m=(lo+hi)/2;
vector<int>z;
for(int i=lo;i<=m;i++){
z.push_back(x[i]);
}
if(are_connected(z, {i})){
hi=m;
} else{
lo=m+1;
}
}
vector<int>z;
for(int i=lo+1;i<x.size();i++){
z.push_back(x[i]);
}
for(int i=0;i<=lo;i++){
z.push_back(x[i]);
}
z.push_back(i);
x=z;
continue;
}
if(!are_connected(x,{i})){
y.push_back(i);
continue;
}
if(!are_connected(y,{i})){
x.push_back(i);
continue;
}
int i0=0;
int j0=0;
if(!are_connected({x[0]},{i})){
int lo=1;
int hi=x.size()-1;
while(lo<hi){
int m=(lo+hi)/2;
vector<int>z;
for(int i=lo;i<=m;i++){
z.push_back(x[i]);
}
if(are_connected(z, {i})){
hi=m;
} else{
lo=m+1;
}
}
i0=lo;
} else {
int lo=0;
int hi=y.size()-1;
while(lo<hi){
int m=(lo+hi)/2;
vector<int>z;
for(int i=lo;i<=m;i++){
z.push_back(y[i]);
}
if(are_connected(z, {i})){
hi=m;
} else{
lo=m+1;
}
}
j0=lo;
}
swap(x[i0], x.back());
swap(y[j0], y[0]);
x.push_back(i);
for(int i: y){
x.push_back(i);
}
y={};
}
if(x.size()>y.size()){
return x;
} else{
return y;
}
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:48:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int i=lo+1;i<x.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
invalid array |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |