#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 = 10;
vec<int> best;
while (T--) {
auto g = attempt();
if (len(g) > len(best))
best = g;
if(len(best) == n) break;
}
return best;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
13 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
340 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
7 ms |
344 KB |
Output is correct |
11 |
Correct |
9 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
344 KB |
Output is correct |
13 |
Correct |
6 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
21 ms |
344 KB |
Output is correct |
3 |
Correct |
34 ms |
344 KB |
Output is correct |
4 |
Correct |
46 ms |
344 KB |
Output is correct |
5 |
Correct |
59 ms |
600 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
20 ms |
344 KB |
Output is correct |
8 |
Correct |
48 ms |
344 KB |
Output is correct |
9 |
Correct |
48 ms |
344 KB |
Output is correct |
10 |
Correct |
57 ms |
484 KB |
Output is correct |
11 |
Correct |
82 ms |
344 KB |
Output is correct |
12 |
Correct |
65 ms |
344 KB |
Output is correct |
13 |
Correct |
67 ms |
600 KB |
Output is correct |
14 |
Correct |
64 ms |
344 KB |
Output is correct |
15 |
Correct |
79 ms |
344 KB |
Output is correct |
16 |
Correct |
33 ms |
344 KB |
Output is correct |
17 |
Correct |
31 ms |
344 KB |
Output is correct |
18 |
Correct |
40 ms |
344 KB |
Output is correct |
19 |
Correct |
47 ms |
340 KB |
Output is correct |
20 |
Correct |
55 ms |
452 KB |
Output is correct |
21 |
Correct |
62 ms |
344 KB |
Output is correct |
22 |
Correct |
72 ms |
344 KB |
Output is correct |
23 |
Correct |
69 ms |
600 KB |
Output is correct |
24 |
Correct |
90 ms |
344 KB |
Output is correct |
25 |
Correct |
86 ms |
344 KB |
Output is correct |
26 |
Correct |
58 ms |
344 KB |
Output is correct |
27 |
Correct |
207 ms |
344 KB |
Output is correct |
28 |
Correct |
185 ms |
596 KB |
Output is correct |
29 |
Correct |
205 ms |
344 KB |
Output is correct |
30 |
Correct |
461 ms |
600 KB |
Output is correct |
31 |
Correct |
324 ms |
344 KB |
Output is correct |
32 |
Correct |
250 ms |
344 KB |
Output is correct |
33 |
Correct |
550 ms |
344 KB |
Output is correct |
34 |
Correct |
511 ms |
596 KB |
Output is correct |
35 |
Correct |
517 ms |
344 KB |
Output is correct |
36 |
Incorrect |
386 ms |
728 KB |
too many elements |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
17 ms |
344 KB |
Output is correct |
3 |
Correct |
37 ms |
344 KB |
Output is correct |
4 |
Partially correct |
60 ms |
648 KB |
Output is partially correct |
5 |
Partially correct |
77 ms |
744 KB |
Output is partially correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
22 ms |
344 KB |
Output is correct |
8 |
Correct |
39 ms |
344 KB |
Output is correct |
9 |
Partially correct |
50 ms |
600 KB |
Output is partially correct |
10 |
Partially correct |
70 ms |
632 KB |
Output is partially correct |
11 |
Partially correct |
67 ms |
984 KB |
Output is partially correct |
12 |
Partially correct |
70 ms |
344 KB |
Output is partially correct |
13 |
Partially correct |
77 ms |
612 KB |
Output is partially correct |
14 |
Correct |
66 ms |
344 KB |
Output is correct |
15 |
Correct |
71 ms |
344 KB |
Output is correct |
16 |
Correct |
22 ms |
344 KB |
Output is correct |
17 |
Correct |
41 ms |
344 KB |
Output is correct |
18 |
Correct |
49 ms |
600 KB |
Output is correct |
19 |
Partially correct |
44 ms |
344 KB |
Output is partially correct |
20 |
Partially correct |
43 ms |
344 KB |
Output is partially correct |
21 |
Correct |
89 ms |
596 KB |
Output is correct |
22 |
Correct |
59 ms |
344 KB |
Output is correct |
23 |
Correct |
190 ms |
344 KB |
Output is correct |
24 |
Correct |
195 ms |
344 KB |
Output is correct |
25 |
Correct |
181 ms |
344 KB |
Output is correct |
26 |
Partially correct |
450 ms |
344 KB |
Output is partially correct |
27 |
Partially correct |
335 ms |
344 KB |
Output is partially correct |
28 |
Partially correct |
244 ms |
344 KB |
Output is partially correct |
29 |
Partially correct |
523 ms |
344 KB |
Output is partially correct |
30 |
Partially correct |
512 ms |
600 KB |
Output is partially correct |
31 |
Partially correct |
518 ms |
344 KB |
Output is partially correct |
32 |
Correct |
11 ms |
344 KB |
Output is correct |
33 |
Correct |
14 ms |
344 KB |
Output is correct |
34 |
Correct |
51 ms |
344 KB |
Output is correct |
35 |
Correct |
36 ms |
344 KB |
Output is correct |
36 |
Correct |
43 ms |
344 KB |
Output is correct |
37 |
Incorrect |
36 ms |
344 KB |
Incorrect |
38 |
Halted |
0 ms |
0 KB |
- |