#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++){
if(!are_connected({B[i]}, A)) continue;
int l = 0, r = na-1;
while(l < r){
int m = (l+r)/2;
vector<int> s;
for(int j = 0; j <= m; j++) s.push_back(A[j]);
if(are_connected({B[i]}, s)) r = m;
else l = m+1;
}
if(max(i+1, nb-i)+max(l+1, na-l) > ans){
ans = max(i+1, nb-i)+max(l+1, na-l), x = l, y = i;
}
l = 0, r = na-1;
while(l < r){
int m = (l+r+1)/2;
vector<int> s;
for(int j = m; j < na; j++) s.push_back(A[j]);
if(are_connected({B[i]}, s)) l = m;
else r = m-1;
}
if(max(i+1, nb-i)+max(l+1, na-l) > ans){
ans = max(i+1, nb-i)+max(l+1, na-l), x = l, 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(B[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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
7 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 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 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 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 |
4 ms |
600 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
440 KB |
Output is correct |
14 |
Correct |
10 ms |
364 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Correct |
15 ms |
344 KB |
Output is correct |
17 |
Correct |
9 ms |
600 KB |
Output is correct |
18 |
Correct |
9 ms |
600 KB |
Output is correct |
19 |
Correct |
6 ms |
600 KB |
Output is correct |
20 |
Correct |
8 ms |
600 KB |
Output is correct |
21 |
Correct |
6 ms |
436 KB |
Output is correct |
22 |
Correct |
8 ms |
600 KB |
Output is correct |
23 |
Correct |
9 ms |
440 KB |
Output is correct |
24 |
Correct |
8 ms |
600 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
11 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
344 KB |
Output is correct |
31 |
Correct |
7 ms |
344 KB |
Output is correct |
32 |
Correct |
11 ms |
344 KB |
Output is correct |
33 |
Correct |
4 ms |
344 KB |
Output is correct |
34 |
Correct |
5 ms |
344 KB |
Output is correct |
35 |
Correct |
5 ms |
344 KB |
Output is correct |
36 |
Correct |
5 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
344 KB |
Output is correct |
38 |
Correct |
9 ms |
600 KB |
Output is correct |
39 |
Correct |
11 ms |
344 KB |
Output is correct |
40 |
Correct |
12 ms |
600 KB |
Output is correct |
41 |
Correct |
13 ms |
344 KB |
Output is correct |
42 |
Correct |
13 ms |
344 KB |
Output is correct |
43 |
Correct |
12 ms |
348 KB |
Output is correct |
44 |
Correct |
16 ms |
352 KB |
Output is correct |
45 |
Correct |
10 ms |
352 KB |
Output is correct |
46 |
Correct |
14 ms |
352 KB |
Output is correct |
47 |
Correct |
11 ms |
352 KB |
Output is correct |
48 |
Correct |
14 ms |
352 KB |
Output is correct |
49 |
Correct |
9 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
432 KB |
Output is correct |
51 |
Correct |
10 ms |
432 KB |
Output is correct |
52 |
Correct |
11 ms |
596 KB |
Output is correct |
53 |
Correct |
6 ms |
444 KB |
Output is correct |
54 |
Correct |
6 ms |
436 KB |
Output is correct |
55 |
Correct |
7 ms |
444 KB |
Output is correct |
56 |
Correct |
6 ms |
708 KB |
Output is correct |
57 |
Correct |
7 ms |
452 KB |
Output is correct |
58 |
Correct |
9 ms |
696 KB |
Output is correct |
59 |
Correct |
12 ms |
600 KB |
Output is correct |
60 |
Correct |
12 ms |
604 KB |
Output is correct |
61 |
Correct |
13 ms |
600 KB |
Output is correct |
62 |
Correct |
12 ms |
604 KB |
Output is correct |
63 |
Correct |
14 ms |
600 KB |
Output is correct |
64 |
Correct |
14 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
6 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 |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
9 ms |
344 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Correct |
13 ms |
344 KB |
Output is correct |
17 |
Correct |
12 ms |
344 KB |
Output is correct |
18 |
Correct |
9 ms |
600 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
8 ms |
600 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
9 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
7 ms |
344 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
10 ms |
352 KB |
Output is correct |
29 |
Correct |
4 ms |
352 KB |
Output is correct |
30 |
Correct |
6 ms |
548 KB |
Output is correct |
31 |
Correct |
5 ms |
344 KB |
Output is correct |
32 |
Correct |
8 ms |
344 KB |
Output is correct |
33 |
Correct |
10 ms |
344 KB |
Output is correct |
34 |
Correct |
9 ms |
344 KB |
Output is correct |
35 |
Correct |
10 ms |
344 KB |
Output is correct |
36 |
Correct |
9 ms |
344 KB |
Output is correct |
37 |
Correct |
7 ms |
440 KB |
Output is correct |
38 |
Correct |
11 ms |
436 KB |
Output is correct |
39 |
Correct |
16 ms |
344 KB |
Output is correct |
40 |
Correct |
6 ms |
436 KB |
Output is correct |
41 |
Correct |
6 ms |
436 KB |
Output is correct |
42 |
Correct |
7 ms |
444 KB |
Output is correct |
43 |
Partially correct |
8 ms |
436 KB |
Output is partially correct |
44 |
Partially correct |
9 ms |
856 KB |
Output is partially correct |
45 |
Partially correct |
9 ms |
600 KB |
Output is partially correct |
46 |
Partially correct |
8 ms |
440 KB |
Output is partially correct |
47 |
Correct |
6 ms |
344 KB |
Output is correct |
48 |
Correct |
6 ms |
344 KB |
Output is correct |
49 |
Correct |
8 ms |
344 KB |
Output is correct |
50 |
Partially correct |
14 ms |
344 KB |
Output is partially correct |
51 |
Partially correct |
13 ms |
344 KB |
Output is partially correct |
52 |
Partially correct |
13 ms |
344 KB |
Output is partially correct |
53 |
Partially correct |
14 ms |
344 KB |
Output is partially correct |
54 |
Partially correct |
15 ms |
344 KB |
Output is partially correct |
55 |
Partially correct |
15 ms |
344 KB |
Output is partially correct |
56 |
Correct |
7 ms |
600 KB |
Output is correct |
57 |
Correct |
8 ms |
344 KB |
Output is correct |
58 |
Partially correct |
11 ms |
344 KB |
Output is partially correct |
59 |
Partially correct |
12 ms |
344 KB |
Output is partially correct |
60 |
Partially correct |
12 ms |
344 KB |
Output is partially correct |
61 |
Partially correct |
15 ms |
344 KB |
Output is partially correct |
62 |
Correct |
6 ms |
960 KB |
Output is correct |
63 |
Correct |
7 ms |
868 KB |
Output is correct |
64 |
Correct |
8 ms |
700 KB |
Output is correct |
65 |
Partially correct |
11 ms |
604 KB |
Output is partially correct |
66 |
Partially correct |
13 ms |
692 KB |
Output is partially correct |
67 |
Partially correct |
15 ms |
436 KB |
Output is partially correct |
68 |
Partially correct |
13 ms |
600 KB |
Output is partially correct |
69 |
Partially correct |
15 ms |
600 KB |
Output is partially correct |
70 |
Partially correct |
15 ms |
688 KB |
Output is partially correct |
71 |
Correct |
7 ms |
448 KB |
Output is correct |
72 |
Correct |
9 ms |
608 KB |
Output is correct |
73 |
Partially correct |
11 ms |
856 KB |
Output is partially correct |
74 |
Partially correct |
14 ms |
696 KB |
Output is partially correct |
75 |
Partially correct |
16 ms |
436 KB |
Output is partially correct |
76 |
Partially correct |
17 ms |
600 KB |
Output is partially correct |
77 |
Correct |
6 ms |
704 KB |
Output is correct |
78 |
Correct |
11 ms |
700 KB |
Output is correct |
79 |
Partially correct |
11 ms |
696 KB |
Output is partially correct |
80 |
Partially correct |
16 ms |
604 KB |
Output is partially correct |
81 |
Partially correct |
13 ms |
600 KB |
Output is partially correct |
82 |
Partially correct |
13 ms |
692 KB |
Output is partially correct |