#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...) 42
#endif
using ll = long long;
#define all(v) v.begin(), v.end()
#define len(v) int(v.size())
const ll INF = 1e18;
bool are_connected(vector<int> A, vector<int> B);
vector<int> longest_trip(int N, int D) {
if (D == 3) {
vector<int> res(N);
iota(all(res), 0);
return res;
}
set<vector<int>> p;
for (int i = 0; i < N; i++) p.insert({i});
while (len(p) > 2) {
auto v = *p.begin(), u = *next(p.begin()), w = *next(next(p.begin()));
vector<int> x;
if (are_connected({v[0]}, {u[0]})) {
x = v;
reverse(all(x));
for (int el : u) x.push_back(el);
p.erase(v);
p.erase(u);
}
else if (are_connected({v[0]}, {w[0]})) {
x = v;
reverse(all(x));
for (int el : w) x.push_back(el);
p.erase(v);
p.erase(w);
}
else {
x = u;
reverse(all(x));
for (int el : w) x.push_back(el);
p.erase(u);
p.erase(w);
}
p.insert(x);
}
vector a = *p.begin(), b = *prev(p.end());
if (len(a) < len(b)) swap(a, b);
int best_i = -1, best_j = -1, best = len(a);
for (int i = 0; i < len(a); i++) {
for (int j = 0; j < len(b); j++) {
int v = a[i], u = b[j];
if (are_connected({v}, {u})) {
int l = max(i + 1, len(a) - i) + max(j + 1, len(b) - j);
if (l > best) {
best_i = i;
best_j = j;
best = l;
}
}
}
}
if (best_i == -1) {
return a;
}
else {
vector<int> x;
if (best_i + 1 > len(a) - best_i) {
for (int i = 0; i <= best_i; i++) {
x.push_back(a[i]);
}
}
else {
for (int i = len(a) - 1; i >= best_i; i--) {
x.push_back(a[i]);
}
}
if (best_j + 1 > len(b) - best_j) {
for (int j = best_j; j >= 0; j--) {
x.push_back(b[j]);
}
}
else {
for (int j = best_j; j < len(b); j++) {
x.push_back(b[j]);
}
}
return x;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 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 |
0 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 |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
344 KB |
Output is correct |
4 |
Correct |
12 ms |
344 KB |
Output is correct |
5 |
Correct |
9 ms |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
11 ms |
344 KB |
Output is correct |
8 |
Correct |
11 ms |
344 KB |
Output is correct |
9 |
Correct |
9 ms |
344 KB |
Output is correct |
10 |
Correct |
12 ms |
344 KB |
Output is correct |
11 |
Correct |
9 ms |
344 KB |
Output is correct |
12 |
Correct |
9 ms |
600 KB |
Output is correct |
13 |
Correct |
9 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
344 KB |
Output is correct |
3 |
Correct |
10 ms |
344 KB |
Output is correct |
4 |
Correct |
8 ms |
344 KB |
Output is correct |
5 |
Correct |
12 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
12 ms |
344 KB |
Output is correct |
8 |
Correct |
10 ms |
344 KB |
Output is correct |
9 |
Correct |
12 ms |
344 KB |
Output is correct |
10 |
Correct |
12 ms |
600 KB |
Output is correct |
11 |
Correct |
10 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
344 KB |
Output is correct |
13 |
Correct |
9 ms |
600 KB |
Output is correct |
14 |
Correct |
9 ms |
344 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Correct |
16 ms |
344 KB |
Output is correct |
17 |
Correct |
10 ms |
344 KB |
Output is correct |
18 |
Correct |
10 ms |
344 KB |
Output is correct |
19 |
Correct |
15 ms |
344 KB |
Output is correct |
20 |
Correct |
12 ms |
468 KB |
Output is correct |
21 |
Correct |
12 ms |
600 KB |
Output is correct |
22 |
Correct |
8 ms |
344 KB |
Output is correct |
23 |
Correct |
10 ms |
524 KB |
Output is correct |
24 |
Correct |
9 ms |
344 KB |
Output is correct |
25 |
Correct |
8 ms |
344 KB |
Output is correct |
26 |
Correct |
9 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
9 ms |
344 KB |
Output is correct |
29 |
Correct |
11 ms |
344 KB |
Output is correct |
30 |
Correct |
11 ms |
344 KB |
Output is correct |
31 |
Correct |
58 ms |
344 KB |
Output is correct |
32 |
Correct |
86 ms |
344 KB |
Output is correct |
33 |
Correct |
19 ms |
344 KB |
Output is correct |
34 |
Correct |
19 ms |
344 KB |
Output is correct |
35 |
Correct |
29 ms |
344 KB |
Output is correct |
36 |
Correct |
11 ms |
340 KB |
Output is correct |
37 |
Correct |
60 ms |
344 KB |
Output is correct |
38 |
Correct |
199 ms |
496 KB |
Output is correct |
39 |
Correct |
308 ms |
484 KB |
Output is correct |
40 |
Correct |
308 ms |
600 KB |
Output is correct |
41 |
Correct |
300 ms |
500 KB |
Output is correct |
42 |
Correct |
352 ms |
496 KB |
Output is correct |
43 |
Correct |
353 ms |
748 KB |
Output is correct |
44 |
Correct |
367 ms |
468 KB |
Output is correct |
45 |
Correct |
10 ms |
344 KB |
Output is correct |
46 |
Correct |
9 ms |
344 KB |
Output is correct |
47 |
Correct |
13 ms |
340 KB |
Output is correct |
48 |
Correct |
13 ms |
344 KB |
Output is correct |
49 |
Correct |
9 ms |
344 KB |
Output is correct |
50 |
Correct |
23 ms |
344 KB |
Output is correct |
51 |
Correct |
57 ms |
436 KB |
Output is correct |
52 |
Correct |
68 ms |
676 KB |
Output is correct |
53 |
Correct |
14 ms |
344 KB |
Output is correct |
54 |
Correct |
24 ms |
344 KB |
Output is correct |
55 |
Correct |
49 ms |
600 KB |
Output is correct |
56 |
Correct |
79 ms |
344 KB |
Output is correct |
57 |
Correct |
54 ms |
344 KB |
Output is correct |
58 |
Correct |
173 ms |
496 KB |
Output is correct |
59 |
Correct |
237 ms |
744 KB |
Output is correct |
60 |
Correct |
327 ms |
592 KB |
Output is correct |
61 |
Correct |
365 ms |
496 KB |
Output is correct |
62 |
Correct |
272 ms |
504 KB |
Output is correct |
63 |
Correct |
328 ms |
484 KB |
Output is correct |
64 |
Correct |
362 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
344 KB |
Output is correct |
2 |
Correct |
13 ms |
340 KB |
Output is correct |
3 |
Correct |
11 ms |
344 KB |
Output is correct |
4 |
Correct |
9 ms |
344 KB |
Output is correct |
5 |
Partially correct |
9 ms |
344 KB |
Output is partially correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
10 ms |
344 KB |
Output is correct |
9 |
Correct |
8 ms |
344 KB |
Output is correct |
10 |
Partially correct |
12 ms |
548 KB |
Output is partially correct |
11 |
Partially correct |
11 ms |
540 KB |
Output is partially correct |
12 |
Partially correct |
11 ms |
524 KB |
Output is partially correct |
13 |
Partially correct |
13 ms |
344 KB |
Output is partially correct |
14 |
Correct |
5 ms |
344 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Incorrect |
1 ms |
344 KB |
Incorrect |
17 |
Halted |
0 ms |
0 KB |
- |