#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
mt19937 mt(123);
bool my_are_connected(vector<int> a, vector<int> b) {
a.erase(unique(begin(a), end(a)), end(a));
b.erase(unique(begin(b), end(b)), end(b));
return are_connected(a, b);
}
pair<int, int> cbin(vector<int> as, vector<int> bs) {
shuffle(begin(as), end(as), mt);
shuffle(begin(bs), end(bs), mt);
bool swp = false;
if (as.size() > bs.size()) {
swap(as, bs);
swp = true;
}
pair<int, int> ret;
if (bs.size() == 1)
ret = make_pair(as[0], bs[0]);
else {
vector<int> xs(begin(bs), begin(bs) + bs.size() / 2), ys(begin(bs) + bs.size() / 2, end(bs));
if (my_are_connected(as, xs))
ret = cbin(as, xs);
else
ret = cbin(as, ys);
}
if (swp) swap(ret.first, ret.second);
return ret;
}
std::vector<int> longest_trip(int N, int D) {
mt = mt19937(123 + N + D);
vector<int> perm(N);
iota(begin(perm), end(perm), 0);
shuffle(begin(perm), end(perm), mt);
vector<int> xs = {}, ys = {};
for (auto x : perm) {
if (xs.empty())
xs.push_back(x);
else if (ys.empty()) {
if (my_are_connected({x}, {xs.back()}))
xs.push_back(x);
else
ys.push_back(x);
} else {
if (bernoulli_distribution(0.5)(mt)) {
if (!my_are_connected({x}, {xs.back()}))
ys.push_back(x);
else if (!my_are_connected({x}, {ys.back()}))
xs.push_back(x);
else {
xs.push_back(x);
xs.insert(end(xs), begin(ys), end(ys));
ys = {};
}
} else {
if (!my_are_connected({x}, {ys.back()}))
xs.push_back(x);
else if (!my_are_connected({x}, {xs.back()}))
ys.push_back(x);
else {
xs.push_back(x);
xs.insert(end(xs), begin(ys), end(ys));
ys = {};
}
}
}
}
vector<int> ret = {};
if (ys.empty())
ret = xs;
else {
if (!my_are_connected(xs, ys))
ret = (xs.size() < ys.size() ? ys : xs);
else if (my_are_connected({xs.front(), xs.back()}, {ys.front(), ys.back()})) {
auto [x, y] = cbin({xs.front(), xs.back()}, {ys.front(), ys.back()});
if (xs.back() != x) reverse(begin(xs), end(xs));
if (ys.front() != y) reverse(begin(ys), end(ys));
xs.insert(end(xs), begin(ys), end(ys));
ret = xs;
} else {
auto [x, y] = cbin(xs, ys);
rotate(begin(xs), find(begin(xs), end(xs), x), end(xs));
rotate(begin(ys), find(begin(ys), end(ys), y), end(ys));
reverse(begin(xs), end(xs));
xs.insert(end(xs), begin(ys), end(ys));
ret = xs;
}
}
return ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
3 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
208 KB |
Output is correct |
2 |
Correct |
8 ms |
208 KB |
Output is correct |
3 |
Correct |
11 ms |
208 KB |
Output is correct |
4 |
Correct |
5 ms |
208 KB |
Output is correct |
5 |
Correct |
8 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
208 KB |
Output is correct |
2 |
Correct |
10 ms |
208 KB |
Output is correct |
3 |
Correct |
9 ms |
208 KB |
Output is correct |
4 |
Correct |
9 ms |
208 KB |
Output is correct |
5 |
Correct |
6 ms |
304 KB |
Output is correct |
6 |
Correct |
13 ms |
208 KB |
Output is correct |
7 |
Correct |
9 ms |
208 KB |
Output is correct |
8 |
Correct |
9 ms |
208 KB |
Output is correct |
9 |
Correct |
4 ms |
304 KB |
Output is correct |
10 |
Correct |
10 ms |
208 KB |
Output is correct |
11 |
Correct |
10 ms |
208 KB |
Output is correct |
12 |
Correct |
10 ms |
308 KB |
Output is correct |
13 |
Correct |
10 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
208 KB |
Output is correct |
2 |
Correct |
10 ms |
208 KB |
Output is correct |
3 |
Correct |
9 ms |
208 KB |
Output is correct |
4 |
Correct |
9 ms |
208 KB |
Output is correct |
5 |
Correct |
6 ms |
208 KB |
Output is correct |
6 |
Correct |
13 ms |
208 KB |
Output is correct |
7 |
Correct |
12 ms |
208 KB |
Output is correct |
8 |
Correct |
8 ms |
208 KB |
Output is correct |
9 |
Correct |
10 ms |
228 KB |
Output is correct |
10 |
Correct |
9 ms |
208 KB |
Output is correct |
11 |
Correct |
8 ms |
208 KB |
Output is correct |
12 |
Correct |
4 ms |
304 KB |
Output is correct |
13 |
Correct |
7 ms |
208 KB |
Output is correct |
14 |
Correct |
18 ms |
208 KB |
Output is correct |
15 |
Incorrect |
7 ms |
208 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
208 KB |
Output is correct |
2 |
Correct |
7 ms |
208 KB |
Output is correct |
3 |
Correct |
6 ms |
208 KB |
Output is correct |
4 |
Correct |
6 ms |
308 KB |
Output is correct |
5 |
Correct |
9 ms |
304 KB |
Output is correct |
6 |
Correct |
12 ms |
208 KB |
Output is correct |
7 |
Correct |
8 ms |
208 KB |
Output is correct |
8 |
Correct |
8 ms |
208 KB |
Output is correct |
9 |
Correct |
8 ms |
308 KB |
Output is correct |
10 |
Correct |
9 ms |
208 KB |
Output is correct |
11 |
Correct |
7 ms |
208 KB |
Output is correct |
12 |
Correct |
10 ms |
208 KB |
Output is correct |
13 |
Correct |
7 ms |
208 KB |
Output is correct |
14 |
Correct |
14 ms |
208 KB |
Output is correct |
15 |
Incorrect |
5 ms |
208 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |