#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(time(0));
vector<int> longest_trip(int n, int d) {
auto Ask = [&](vector<int> a, vector<int> b) {
return are_connected(a, b);
};
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
shuffle(idx.begin(), idx.end(), rng);
vector<int> pa, pb;
pa.push_back(idx[0]);
pb.push_back(idx[1]);
bool know = false;
for (int ver = 2; ver < n; ver++) {
int i = idx[ver];
if (know) {
if (Ask({i}, {pa.back()})) {
pa.push_back(i);
know = false;
continue;
} else {
pb.push_back(i);
know = true;
continue;
}
}
if (Ask({i}, {pa.back()})) {
pa.push_back(i);
know = false;
continue;
}
if (Ask({i}, {pb.back()})) {
pb.push_back(i);
know = true;
continue;
}
while (!pb.empty()) {
pa.push_back(pb.back());
pb.pop_back();
}
pb.push_back(i);
know = false;
}
if (pa.empty()) {
return pb;
}
if (pb.empty()) {
return pa;
}
if (!Ask(pa, pb)) {
if ((int) pa.size() < (int) pb.size()) {
swap(pa, pb);
}
return pa;
}
vector<int> L, R;
L.push_back(pa[0]);
if ((int) pa.size() > 1) {
L.push_back(pa.back());
}
R.push_back(pb[0]);
if ((int) pb.size() > 1) {
R.push_back(pb.back());
}
if (Ask(L, R)) {
if (Ask({pa[0]}, {pb[0]})) {
reverse(pa.begin(), pa.end());
vector<int> res;
for (int i : pa) {
res.push_back(i);
}
for (int i : pb) {
res.push_back(i);
}
return res;
}
if (Ask({pa.back()}, {pb[0]})) {
vector<int> res;
for (int i : pa) {
res.push_back(i);
}
for (int i : pb) {
res.push_back(i);
}
return res;
}
if (Ask({pa[0]}, {pb.back()})) {
vector<int> res;
for (int i : pb) {
res.push_back(i);
}
for (int i : pa) {
res.push_back(i);
}
return res;
}
reverse(pa.begin(), pa.end());
vector<int> res;
for (int i : pb) {
res.push_back(i);
}
for (int i : pa) {
res.push_back(i);
}
return res;
}
int x = -1, y = -1;
{
int low = 0, high = (int) pa.size() - 1;
while (low <= high) {
int mid = (low + high) >> 1;
vector<int> qv;
for (int i = 0; i <= mid; i++) {
qv.push_back(pa[i]);
}
if (Ask(qv, pb)) {
x = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
}
{
int low = 0, high = (int) pb.size() - 1;
while (low <= high) {
int mid = (low + high) >> 1;
vector<int> qv;
for (int i = 0; i <= mid; i++) {
qv.push_back(pb[i]);
}
if (Ask({pa[x]}, qv)) {
y = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
}
int ca = (int) pa.size();
int cb = (int) pb.size();
vector<int> new_a;
for (int i = 0; i < ca; i++) {
new_a.push_back(pa[(i + 1 + x) % ca]);
}
pa = new_a;
vector<int> new_b;
for (int i = 0; i < cb; i++) {
new_b.push_back(pb[(i + 1 + y) % cb]);
}
pb = new_b;
reverse(pb.begin(), pb.end());
vector<int> res;
for (int i : pa) {
res.push_back(i);
}
for (int i : pb) {
res.push_back(i);
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
7 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 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 |
6 ms |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
7 ms |
344 KB |
Output is correct |
9 |
Correct |
6 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 |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
344 KB |
Output is correct |
11 |
Correct |
8 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
344 KB |
Output is correct |
13 |
Correct |
6 ms |
344 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
596 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
7 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
344 KB |
Output is correct |
20 |
Correct |
7 ms |
344 KB |
Output is correct |
21 |
Correct |
4 ms |
344 KB |
Output is correct |
22 |
Correct |
7 ms |
344 KB |
Output is correct |
23 |
Correct |
8 ms |
344 KB |
Output is correct |
24 |
Correct |
8 ms |
344 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
596 KB |
Output is correct |
27 |
Correct |
8 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
5 ms |
344 KB |
Output is correct |
31 |
Correct |
8 ms |
344 KB |
Output is correct |
32 |
Correct |
6 ms |
344 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
34 |
Correct |
6 ms |
344 KB |
Output is correct |
35 |
Correct |
5 ms |
344 KB |
Output is correct |
36 |
Correct |
6 ms |
344 KB |
Output is correct |
37 |
Correct |
5 ms |
344 KB |
Output is correct |
38 |
Correct |
6 ms |
344 KB |
Output is correct |
39 |
Correct |
8 ms |
344 KB |
Output is correct |
40 |
Correct |
7 ms |
344 KB |
Output is correct |
41 |
Correct |
7 ms |
344 KB |
Output is correct |
42 |
Correct |
7 ms |
344 KB |
Output is correct |
43 |
Correct |
7 ms |
344 KB |
Output is correct |
44 |
Correct |
4 ms |
344 KB |
Output is correct |
45 |
Correct |
6 ms |
344 KB |
Output is correct |
46 |
Correct |
7 ms |
344 KB |
Output is correct |
47 |
Correct |
8 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
344 KB |
Output is correct |
49 |
Correct |
10 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
344 KB |
Output is correct |
51 |
Correct |
7 ms |
344 KB |
Output is correct |
52 |
Correct |
6 ms |
344 KB |
Output is correct |
53 |
Correct |
7 ms |
344 KB |
Output is correct |
54 |
Correct |
8 ms |
344 KB |
Output is correct |
55 |
Correct |
6 ms |
344 KB |
Output is correct |
56 |
Correct |
5 ms |
344 KB |
Output is correct |
57 |
Correct |
6 ms |
344 KB |
Output is correct |
58 |
Correct |
7 ms |
460 KB |
Output is correct |
59 |
Correct |
8 ms |
452 KB |
Output is correct |
60 |
Correct |
6 ms |
344 KB |
Output is correct |
61 |
Correct |
7 ms |
344 KB |
Output is correct |
62 |
Correct |
7 ms |
344 KB |
Output is correct |
63 |
Correct |
7 ms |
452 KB |
Output is correct |
64 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 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 |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
8 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 |
6 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
600 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
344 KB |
Output is correct |
17 |
Correct |
9 ms |
432 KB |
Output is correct |
18 |
Correct |
7 ms |
344 KB |
Output is correct |
19 |
Correct |
8 ms |
344 KB |
Output is correct |
20 |
Correct |
7 ms |
448 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
5 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 |
6 ms |
344 KB |
Output is correct |
26 |
Correct |
8 ms |
344 KB |
Output is correct |
27 |
Correct |
9 ms |
344 KB |
Output is correct |
28 |
Correct |
8 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
440 KB |
Output is correct |
31 |
Correct |
5 ms |
344 KB |
Output is correct |
32 |
Correct |
11 ms |
344 KB |
Output is correct |
33 |
Correct |
11 ms |
344 KB |
Output is correct |
34 |
Correct |
8 ms |
344 KB |
Output is correct |
35 |
Correct |
8 ms |
344 KB |
Output is correct |
36 |
Correct |
7 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
344 KB |
Output is correct |
38 |
Correct |
7 ms |
344 KB |
Output is correct |
39 |
Correct |
8 ms |
344 KB |
Output is correct |
40 |
Correct |
5 ms |
600 KB |
Output is correct |
41 |
Correct |
6 ms |
344 KB |
Output is correct |
42 |
Correct |
8 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
448 KB |
Output is correct |
44 |
Correct |
6 ms |
344 KB |
Output is correct |
45 |
Correct |
7 ms |
600 KB |
Output is correct |
46 |
Correct |
6 ms |
344 KB |
Output is correct |
47 |
Correct |
5 ms |
344 KB |
Output is correct |
48 |
Correct |
6 ms |
344 KB |
Output is correct |
49 |
Correct |
6 ms |
344 KB |
Output is correct |
50 |
Correct |
7 ms |
344 KB |
Output is correct |
51 |
Correct |
7 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
7 ms |
344 KB |
Output is correct |
54 |
Correct |
7 ms |
344 KB |
Output is correct |
55 |
Correct |
7 ms |
344 KB |
Output is correct |
56 |
Correct |
7 ms |
344 KB |
Output is correct |
57 |
Correct |
8 ms |
344 KB |
Output is correct |
58 |
Correct |
7 ms |
448 KB |
Output is correct |
59 |
Correct |
7 ms |
344 KB |
Output is correct |
60 |
Correct |
7 ms |
344 KB |
Output is correct |
61 |
Correct |
8 ms |
344 KB |
Output is correct |
62 |
Correct |
9 ms |
460 KB |
Output is correct |
63 |
Correct |
4 ms |
344 KB |
Output is correct |
64 |
Correct |
9 ms |
592 KB |
Output is correct |
65 |
Correct |
9 ms |
600 KB |
Output is correct |
66 |
Correct |
9 ms |
452 KB |
Output is correct |
67 |
Correct |
7 ms |
344 KB |
Output is correct |
68 |
Correct |
6 ms |
344 KB |
Output is correct |
69 |
Correct |
8 ms |
600 KB |
Output is correct |
70 |
Correct |
8 ms |
612 KB |
Output is correct |
71 |
Correct |
7 ms |
452 KB |
Output is correct |
72 |
Correct |
7 ms |
344 KB |
Output is correct |
73 |
Correct |
6 ms |
344 KB |
Output is correct |
74 |
Correct |
11 ms |
452 KB |
Output is correct |
75 |
Correct |
9 ms |
600 KB |
Output is correct |
76 |
Correct |
7 ms |
344 KB |
Output is correct |
77 |
Correct |
7 ms |
452 KB |
Output is correct |
78 |
Correct |
6 ms |
344 KB |
Output is correct |
79 |
Correct |
6 ms |
344 KB |
Output is correct |
80 |
Correct |
9 ms |
344 KB |
Output is correct |
81 |
Correct |
9 ms |
344 KB |
Output is correct |
82 |
Correct |
8 ms |
456 KB |
Output is correct |