#include "longesttrip.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
vector<int> longest_trip(int n, int D){
vector<int> A = {0}, B = {};
for(int i = 1; i < n; i++){
if(B.empty()){
if(are_connected({A.back()}, {i})) A.push_back(i);
else if(are_connected({A[0]}, {i})) A.insert(A.begin(), i);
else B = {i};
}
else{
if(are_connected({A.back()}, {i})) A.push_back(i);
else if(are_connected({B.back()}, {i})) B.push_back(i);
else{
reverse(B.begin(), B.end());
A.insert(A.end(), B.begin(), B.end());
B.clear();
i--;
}
}
if(A.size() < B.size()) swap(A, B);
}
int na = A.size(), nb = B.size();
int ans = nb, x = -1, y = -1;
for(int i = 0; i < nb; i++) for(int j = 0; j < na; j++){
int cur = max(i+1, nb-i)+max(j+1, na-j);
if(cur <= ans) continue;
if(are_connected({B[i]}, {A[j]})) ans = cur, x = j, y = i;
}
vector<int> C = A;
if(x != -1){
C.clear();
if(na > 1 && are_connected({A[0]}, {A.back()})){
for(int i = x+1; i <= na+x; i++) C.push_back(A[i%na]);
}
else if(na-x > x+1){
for(int i = na-1; i >= x; i--) C.push_back(A[i]);
}
else{
for(int i = 0; i <= x; i++) C.push_back(A[i]);
}
if(nb > 1 && are_connected({B[0]}, {B.back()})){
for(int i = y; i < nb+y; i++) C.push_back(A[i%nb]);
}
else if(nb-y > y+1){
for(int i = y; i < nb; i++) C.push_back(B[i]);
}
else{
for(int i = y; i >= 0; i--) C.push_back(B[i]);
}
}
return C;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
340 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Incorrect |
0 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Incorrect |
0 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Incorrect |
0 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |