#include "longesttrip.h"
#include "bits/stdc++.h"
using namespace std;
#define len(x) (int)(x.size())
#define all(x) x.begin(), x.end()
#define inf 1000'000'000'000'000'000LL
#define bit(x, i) (((x) >> i) & 1)
template<typename T>
using vec = vector<T>;
std::vector<int> longest_trip(int n, int d) {
mt19937 rnd(14345);
if (d == 3) {
vec<int> ans(n);
iota(all(ans), 0);
return ans;
}
if (d == 2) {
deque<int> ans;
int cur = 0;
ans.push_back(cur);
set<int> not_used;
for (int i = 1; i < n; i++) {
not_used.insert(i);
}
while (true) {
if (not_used.empty()) break;
vec<int> next(all(not_used));
int nxt = next[0];
if (are_connected({cur}, {nxt})) {
ans.push_back(nxt);
not_used.erase(nxt);
cur = nxt;
} else {
if (next.size() == 1) {
ans.push_front(next[0]);
break;
} else {
int net = next[1];
ans.push_back(net);
not_used.erase(net);
cur = net;
}
}
}
return vec<int>(all(ans));
} else {
auto attempt = [&]() {
deque<int> ans;
int cur = rnd() % n;
ans.push_back(cur);
set<int> not_used;
for (int i = 0; i < n; i++) {
if (i != cur) not_used.insert(i);
}
auto try_find = [&](int v) {
vec<int> t(all(not_used));
if (!are_connected({v}, t)) return -1;
shuffle(all(t), rnd);
int tl = 0;
int tr = len(t) - 1;
auto get_part = [&](int f) {
vec<int> r;
for (int i = 0; i <= f; i++)
r.push_back(t[i]);
return r;
};
while (tl != tr) {
int tm = (tl + tr) / 2;
if (are_connected({v}, get_part(tm))) {
tr = tm;
} else {
tl = tm + 1;
}
}
return t[tl];
};
while (true) {
if (not_used.empty()) break;
int u = try_find(ans.back());
if (u == -1) break;
ans.push_back(u);
not_used.erase(u);
}
while (true) {
if (not_used.empty()) break;
int u = try_find(ans.front());
if (u == -1) break;
ans.push_front(u);
not_used.erase(u);
}
return vec<int>(all(ans));
};
int T = 15;
vec<int> best;
while (T--) {
auto g = attempt();
if (len(g) > len(best))
best = g;
if(len(best) == n) break;
}
return best;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
19 ms |
476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
340 KB |
Output is correct |
5 |
Correct |
8 ms |
344 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
7 ms |
344 KB |
Output is correct |
10 |
Correct |
7 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
10 ms |
600 KB |
Output is correct |
13 |
Correct |
7 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
16 ms |
344 KB |
Output is correct |
3 |
Correct |
37 ms |
344 KB |
Output is correct |
4 |
Correct |
56 ms |
344 KB |
Output is correct |
5 |
Correct |
63 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
21 ms |
344 KB |
Output is correct |
8 |
Correct |
39 ms |
344 KB |
Output is correct |
9 |
Correct |
48 ms |
344 KB |
Output is correct |
10 |
Correct |
78 ms |
1380 KB |
Output is correct |
11 |
Correct |
59 ms |
344 KB |
Output is correct |
12 |
Correct |
78 ms |
344 KB |
Output is correct |
13 |
Correct |
66 ms |
344 KB |
Output is correct |
14 |
Correct |
107 ms |
344 KB |
Output is correct |
15 |
Correct |
134 ms |
344 KB |
Output is correct |
16 |
Correct |
31 ms |
344 KB |
Output is correct |
17 |
Correct |
30 ms |
344 KB |
Output is correct |
18 |
Correct |
34 ms |
600 KB |
Output is correct |
19 |
Correct |
34 ms |
344 KB |
Output is correct |
20 |
Correct |
56 ms |
344 KB |
Output is correct |
21 |
Correct |
72 ms |
600 KB |
Output is correct |
22 |
Correct |
75 ms |
344 KB |
Output is correct |
23 |
Correct |
68 ms |
720 KB |
Output is correct |
24 |
Correct |
91 ms |
616 KB |
Output is correct |
25 |
Correct |
124 ms |
344 KB |
Output is correct |
26 |
Correct |
110 ms |
344 KB |
Output is correct |
27 |
Correct |
294 ms |
344 KB |
Output is correct |
28 |
Correct |
264 ms |
344 KB |
Output is correct |
29 |
Correct |
282 ms |
344 KB |
Output is correct |
30 |
Correct |
633 ms |
344 KB |
Output is correct |
31 |
Correct |
504 ms |
344 KB |
Output is correct |
32 |
Correct |
380 ms |
600 KB |
Output is correct |
33 |
Incorrect |
720 ms |
344 KB |
too many elements |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
344 KB |
Output is correct |
2 |
Correct |
16 ms |
344 KB |
Output is correct |
3 |
Correct |
24 ms |
344 KB |
Output is correct |
4 |
Partially correct |
60 ms |
600 KB |
Output is partially correct |
5 |
Partially correct |
77 ms |
480 KB |
Output is partially correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
16 ms |
344 KB |
Output is correct |
8 |
Correct |
48 ms |
344 KB |
Output is correct |
9 |
Partially correct |
38 ms |
344 KB |
Output is partially correct |
10 |
Partially correct |
75 ms |
600 KB |
Output is partially correct |
11 |
Partially correct |
60 ms |
344 KB |
Output is partially correct |
12 |
Partially correct |
62 ms |
344 KB |
Output is partially correct |
13 |
Partially correct |
71 ms |
344 KB |
Output is partially correct |
14 |
Correct |
122 ms |
344 KB |
Output is correct |
15 |
Correct |
101 ms |
344 KB |
Output is correct |
16 |
Correct |
25 ms |
344 KB |
Output is correct |
17 |
Correct |
30 ms |
344 KB |
Output is correct |
18 |
Correct |
29 ms |
344 KB |
Output is correct |
19 |
Partially correct |
44 ms |
344 KB |
Output is partially correct |
20 |
Partially correct |
45 ms |
344 KB |
Output is partially correct |
21 |
Correct |
125 ms |
344 KB |
Output is correct |
22 |
Correct |
105 ms |
344 KB |
Output is correct |
23 |
Partially correct |
277 ms |
344 KB |
Output is partially correct |
24 |
Partially correct |
297 ms |
344 KB |
Output is partially correct |
25 |
Partially correct |
292 ms |
344 KB |
Output is partially correct |
26 |
Partially correct |
713 ms |
344 KB |
Output is partially correct |
27 |
Partially correct |
487 ms |
344 KB |
Output is partially correct |
28 |
Partially correct |
406 ms |
344 KB |
Output is partially correct |
29 |
Incorrect |
676 ms |
344 KB |
too many elements |
30 |
Halted |
0 ms |
0 KB |
- |