#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
int n, d;
vector<vector<int>> paths;
vector<int> longest_trip(int _n, int _d){
n = _n, d = _d;
paths.clear();
for(int i = 0; i < n; ++i)
paths.push_back({i});
mt19937 mt(34234);
while(paths.size() > 2){
int sz = paths.size();
shuffle(paths.begin(), paths.end(), mt);
vector<int> t{sz - 1, sz - 2, sz - 3};
bool ok = false;
for(int i = 0; i < 3 && !ok; ++i){
for(int j = i + 1; j < 3 && !ok; ++j){
if(i == 1 && j == 2){
swap(paths[t[i]], paths.back());
swap(paths[t[j]], paths[sz - 2]);
ok = true;
break;
}
if(are_connected({paths[t[i]][0]}, {paths[t[j]][0]})){
swap(paths[t[i]], paths.back());
swap(paths[t[j]], paths[sz - 2]);
ok = true;
break;
}
}
}
auto u = paths.back();
paths.pop_back();
auto v = paths.back();
paths.pop_back();
reverse(u.begin(), u.end());
for(int x: v)
u.push_back(x);
paths.push_back(u);
}
if(paths.size() == 1)
return paths[0];
if(paths[1].size() > paths[0].size())
swap(paths[0], paths[1]);
bool conn = false;
if(are_connected({paths[0][0]}, {paths[1][0]})){
conn = true;
}
else if(are_connected({paths[0][0]}, {paths[1].back()})){
reverse(paths[1].begin(), paths[1].end());
conn = true;
}
else if(are_connected({paths[0].back()}, {paths[1][0]})){
reverse(paths[0].begin(), paths[0].end());
conn = true;
}
else if(are_connected({paths[0].back()}, {paths[1].back()})){
reverse(paths[0].begin(), paths[0].end());
reverse(paths[1].begin(), paths[1].end());
conn = true;
}
if(conn){
reverse(paths[0].begin(), paths[0].end());
for(int x: paths[1])
paths[0].push_back(x);
return paths[0];
}
if(!are_connected(paths[0], paths[1]))
return paths[0];
auto get_prefix = [](const vector<int> &v, int idx){
vector<int> ret;
for(int i = 0; i <= idx; ++i)
ret.push_back(v[i]);
return ret;
};
int l1 = 0, r1 = (int)paths[0].size() - 1;
while(l1 != r1){
int mid = (l1 + r1) >> 1;
if(are_connected(get_prefix(paths[0], mid), paths[1]))
r1 = mid;
else
l1 = mid + 1;
}
int l2 = 0, r2 = (int)paths[1].size() - 1;
while(l2 != r2){
int mid = (l2 + r2) >> 1;
if(are_connected({paths[0][l1]}, get_prefix(paths[1], mid)))
r2 = mid;
else
l2 = mid + 1;
}
vector<int> ans;
for(int i = l1 + 1; i < (int)paths[0].size(); ++i)
ans.push_back(paths[0][i]);
for(int i = 0; i <= l1; ++i)
ans.push_back(paths[0][i]);
for(int i = l2; i < (int)paths[1].size(); ++i)
ans.push_back(paths[1][i]);
for(int i = 0; i < l2; ++i)
ans.push_back(paths[1][i]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 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 |
608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 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 |
608 KB |
Output is correct |
6 |
Correct |
9 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 |
8 ms |
864 KB |
Output is correct |
11 |
Correct |
6 ms |
716 KB |
Output is correct |
12 |
Correct |
6 ms |
720 KB |
Output is correct |
13 |
Correct |
6 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 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 |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
460 KB |
Output is correct |
6 |
Correct |
9 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 |
6 ms |
600 KB |
Output is correct |
10 |
Correct |
7 ms |
464 KB |
Output is correct |
11 |
Correct |
6 ms |
716 KB |
Output is correct |
12 |
Correct |
6 ms |
720 KB |
Output is correct |
13 |
Correct |
6 ms |
720 KB |
Output is correct |
14 |
Correct |
13 ms |
344 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
7 ms |
344 KB |
Output is correct |
18 |
Correct |
5 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
7 ms |
616 KB |
Output is correct |
22 |
Correct |
6 ms |
856 KB |
Output is correct |
23 |
Correct |
8 ms |
612 KB |
Output is correct |
24 |
Correct |
6 ms |
600 KB |
Output is correct |
25 |
Correct |
11 ms |
344 KB |
Output is correct |
26 |
Correct |
11 ms |
344 KB |
Output is correct |
27 |
Correct |
11 ms |
344 KB |
Output is correct |
28 |
Correct |
11 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 |
7 ms |
700 KB |
Output is correct |
32 |
Correct |
7 ms |
444 KB |
Output is correct |
33 |
Correct |
6 ms |
604 KB |
Output is correct |
34 |
Correct |
6 ms |
852 KB |
Output is correct |
35 |
Correct |
6 ms |
852 KB |
Output is correct |
36 |
Correct |
6 ms |
728 KB |
Output is correct |
37 |
Correct |
7 ms |
716 KB |
Output is correct |
38 |
Correct |
8 ms |
976 KB |
Output is correct |
39 |
Correct |
9 ms |
608 KB |
Output is correct |
40 |
Correct |
8 ms |
612 KB |
Output is correct |
41 |
Correct |
8 ms |
600 KB |
Output is correct |
42 |
Correct |
10 ms |
724 KB |
Output is correct |
43 |
Correct |
10 ms |
868 KB |
Output is correct |
44 |
Correct |
9 ms |
720 KB |
Output is correct |
45 |
Correct |
10 ms |
344 KB |
Output is correct |
46 |
Correct |
13 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 |
9 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
700 KB |
Output is correct |
51 |
Correct |
8 ms |
700 KB |
Output is correct |
52 |
Correct |
9 ms |
700 KB |
Output is correct |
53 |
Correct |
6 ms |
460 KB |
Output is correct |
54 |
Correct |
7 ms |
612 KB |
Output is correct |
55 |
Correct |
7 ms |
856 KB |
Output is correct |
56 |
Correct |
8 ms |
720 KB |
Output is correct |
57 |
Correct |
8 ms |
724 KB |
Output is correct |
58 |
Correct |
9 ms |
600 KB |
Output is correct |
59 |
Correct |
10 ms |
724 KB |
Output is correct |
60 |
Correct |
9 ms |
724 KB |
Output is correct |
61 |
Correct |
10 ms |
600 KB |
Output is correct |
62 |
Correct |
10 ms |
1112 KB |
Output is correct |
63 |
Correct |
10 ms |
612 KB |
Output is correct |
64 |
Correct |
10 ms |
868 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 |
7 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
460 KB |
Output is correct |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
7 ms |
440 KB |
Output is correct |
9 |
Correct |
6 ms |
600 KB |
Output is correct |
10 |
Correct |
7 ms |
612 KB |
Output is correct |
11 |
Correct |
7 ms |
464 KB |
Output is correct |
12 |
Correct |
8 ms |
708 KB |
Output is correct |
13 |
Correct |
8 ms |
460 KB |
Output is correct |
14 |
Correct |
14 ms |
344 KB |
Output is correct |
15 |
Correct |
11 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
344 KB |
Output is correct |
17 |
Correct |
7 ms |
344 KB |
Output is correct |
18 |
Correct |
7 ms |
600 KB |
Output is correct |
19 |
Correct |
6 ms |
708 KB |
Output is correct |
20 |
Correct |
7 ms |
860 KB |
Output is correct |
21 |
Correct |
14 ms |
344 KB |
Output is correct |
22 |
Correct |
13 ms |
344 KB |
Output is correct |
23 |
Correct |
8 ms |
344 KB |
Output is correct |
24 |
Correct |
9 ms |
344 KB |
Output is correct |
25 |
Correct |
11 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
444 KB |
Output is correct |
27 |
Correct |
8 ms |
696 KB |
Output is correct |
28 |
Correct |
9 ms |
444 KB |
Output is correct |
29 |
Correct |
7 ms |
704 KB |
Output is correct |
30 |
Correct |
7 ms |
708 KB |
Output is correct |
31 |
Correct |
8 ms |
600 KB |
Output is correct |
32 |
Correct |
9 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
344 KB |
Output is correct |
35 |
Correct |
9 ms |
344 KB |
Output is correct |
36 |
Correct |
10 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
444 KB |
Output is correct |
38 |
Correct |
9 ms |
700 KB |
Output is correct |
39 |
Correct |
10 ms |
448 KB |
Output is correct |
40 |
Correct |
7 ms |
456 KB |
Output is correct |
41 |
Correct |
8 ms |
456 KB |
Output is correct |
42 |
Correct |
8 ms |
708 KB |
Output is correct |
43 |
Correct |
7 ms |
600 KB |
Output is correct |
44 |
Correct |
8 ms |
952 KB |
Output is correct |
45 |
Correct |
8 ms |
600 KB |
Output is correct |
46 |
Correct |
8 ms |
856 KB |
Output is correct |
47 |
Correct |
8 ms |
1228 KB |
Output is correct |
48 |
Correct |
8 ms |
720 KB |
Output is correct |
49 |
Correct |
11 ms |
480 KB |
Output is correct |
50 |
Correct |
9 ms |
608 KB |
Output is correct |
51 |
Correct |
10 ms |
468 KB |
Output is correct |
52 |
Partially correct |
10 ms |
600 KB |
Output is partially correct |
53 |
Correct |
10 ms |
616 KB |
Output is correct |
54 |
Correct |
9 ms |
640 KB |
Output is correct |
55 |
Correct |
10 ms |
596 KB |
Output is correct |
56 |
Correct |
8 ms |
732 KB |
Output is correct |
57 |
Correct |
9 ms |
732 KB |
Output is correct |
58 |
Correct |
10 ms |
468 KB |
Output is correct |
59 |
Correct |
9 ms |
868 KB |
Output is correct |
60 |
Partially correct |
10 ms |
600 KB |
Output is partially correct |
61 |
Correct |
10 ms |
628 KB |
Output is correct |
62 |
Correct |
9 ms |
724 KB |
Output is correct |
63 |
Correct |
8 ms |
732 KB |
Output is correct |
64 |
Correct |
9 ms |
600 KB |
Output is correct |
65 |
Correct |
11 ms |
724 KB |
Output is correct |
66 |
Partially correct |
10 ms |
476 KB |
Output is partially correct |
67 |
Partially correct |
11 ms |
624 KB |
Output is partially correct |
68 |
Partially correct |
10 ms |
468 KB |
Output is partially correct |
69 |
Partially correct |
11 ms |
720 KB |
Output is partially correct |
70 |
Partially correct |
10 ms |
600 KB |
Output is partially correct |
71 |
Correct |
9 ms |
708 KB |
Output is correct |
72 |
Correct |
10 ms |
856 KB |
Output is correct |
73 |
Correct |
9 ms |
728 KB |
Output is correct |
74 |
Partially correct |
12 ms |
600 KB |
Output is partially correct |
75 |
Partially correct |
11 ms |
872 KB |
Output is partially correct |
76 |
Partially correct |
11 ms |
464 KB |
Output is partially correct |
77 |
Correct |
9 ms |
856 KB |
Output is correct |
78 |
Correct |
10 ms |
708 KB |
Output is correct |
79 |
Correct |
9 ms |
612 KB |
Output is correct |
80 |
Partially correct |
10 ms |
612 KB |
Output is partially correct |
81 |
Correct |
8 ms |
616 KB |
Output is correct |
82 |
Partially correct |
10 ms |
868 KB |
Output is partially correct |