/*************************************
* author: marvinthang *
* created: 18.01.2024 16:42:53 *
*************************************/
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i-- > 0; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
vector<int> longest_trip(int N, int D) {
auto connected_landmark = [&] (int u, int v) {
return are_connected(vector{u}, vector{v});
};
vector <int> path;
if (D == 3) {
path.resize(N);
iota(ALL(path), 0);
} else if (D == 2) {
if (!connected_landmark(0, 1)) path = vector{0, 2, 1};
else if (!connected_landmark(0, 2)) path = vector{0, 1, 2};
else path = vector{1, 0, 2};
FOR(i, 3, N) {
if (!connected_landmark(i, path.back())) path.insert(path.begin(), i);
else path.push_back(i);
}
} else {
vector <int> left{0}, right{1};
FOR(i, 2, N) {
if (connected_landmark(left.back(), i)) left.push_back(i);
else if (connected_landmark(right.back(), i)) right.push_back(i);
else {
left.insert(left.end(), right.rbegin(), right.rend());
right = vector{i};
}
}
if (left.size() < right.size()) swap(left, right);
if (right.empty() || !are_connected(left, right)) return left;
REP(t, 2) {
if ((int) left.size() > 1) {
if (connected_landmark(left.back(), right.back())) {
left.insert(left.end(), right.rbegin(), right.rend());
return left;
} else if (connected_landmark(left[0], right.back())) {
right.insert(right.end(), ALL(left));
return right;
}
assert(connected_landmark(left[0], left.back()));
}
swap(left, right);
}
int l = 1, r = (int) left.size() - 1;
while (l <= r) {
int m = l + r >> 1;
if (are_connected(vector(left.begin(), left.begin() + m), right)) r = m - 1;
else l = m + 1;
}
rotate(left.begin(), left.begin() + l, left.end());
l = 0, r = (int) right.size() - 2;
while (l <= r) {
int m = l + r >> 1;
if (are_connected(vector(right.begin(), right.begin() + m + 1), {left.back()})) r = m - 1;
else l = m + 1;
}
rotate(right.begin(), right.begin() + l, right.end());
left.insert(left.end(), ALL(right));
return left;
}
return path;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:86:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
86 | int m = l + r >> 1;
| ~~^~~
longesttrip.cpp:93:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
93 | int m = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
5 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 |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
600 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
5 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 |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
8 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 |
4 ms |
600 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
600 KB |
Output is correct |
13 |
Correct |
6 ms |
344 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
10 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
5 ms |
344 KB |
Output is correct |
21 |
Correct |
5 ms |
440 KB |
Output is correct |
22 |
Correct |
5 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
608 KB |
Output is correct |
24 |
Correct |
6 ms |
692 KB |
Output is correct |
25 |
Correct |
6 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
372 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
5 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
5 ms |
344 KB |
Output is correct |
31 |
Correct |
7 ms |
344 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
5 ms |
344 KB |
Output is correct |
34 |
Correct |
5 ms |
344 KB |
Output is correct |
35 |
Correct |
5 ms |
344 KB |
Output is correct |
36 |
Correct |
4 ms |
600 KB |
Output is correct |
37 |
Correct |
5 ms |
344 KB |
Output is correct |
38 |
Correct |
5 ms |
344 KB |
Output is correct |
39 |
Correct |
6 ms |
344 KB |
Output is correct |
40 |
Correct |
6 ms |
344 KB |
Output is correct |
41 |
Correct |
7 ms |
600 KB |
Output is correct |
42 |
Correct |
7 ms |
344 KB |
Output is correct |
43 |
Correct |
7 ms |
344 KB |
Output is correct |
44 |
Correct |
7 ms |
600 KB |
Output is correct |
45 |
Correct |
9 ms |
344 KB |
Output is correct |
46 |
Correct |
11 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 |
7 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
440 KB |
Output is correct |
51 |
Correct |
8 ms |
440 KB |
Output is correct |
52 |
Correct |
8 ms |
344 KB |
Output is correct |
53 |
Correct |
5 ms |
344 KB |
Output is correct |
54 |
Correct |
6 ms |
596 KB |
Output is correct |
55 |
Correct |
6 ms |
344 KB |
Output is correct |
56 |
Correct |
5 ms |
436 KB |
Output is correct |
57 |
Correct |
7 ms |
440 KB |
Output is correct |
58 |
Correct |
8 ms |
608 KB |
Output is correct |
59 |
Correct |
8 ms |
604 KB |
Output is correct |
60 |
Correct |
8 ms |
596 KB |
Output is correct |
61 |
Correct |
9 ms |
600 KB |
Output is correct |
62 |
Correct |
7 ms |
856 KB |
Output is correct |
63 |
Correct |
8 ms |
604 KB |
Output is correct |
64 |
Correct |
9 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
596 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 |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
756 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
600 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
600 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
7 ms |
612 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
6 ms |
600 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
5 ms |
344 KB |
Output is correct |
25 |
Correct |
5 ms |
352 KB |
Output is correct |
26 |
Correct |
4 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
7 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
340 KB |
Output is correct |
30 |
Correct |
5 ms |
344 KB |
Output is correct |
31 |
Correct |
5 ms |
344 KB |
Output is correct |
32 |
Correct |
9 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
8 ms |
344 KB |
Output is correct |
35 |
Correct |
7 ms |
344 KB |
Output is correct |
36 |
Correct |
8 ms |
344 KB |
Output is correct |
37 |
Correct |
5 ms |
436 KB |
Output is correct |
38 |
Correct |
9 ms |
436 KB |
Output is correct |
39 |
Correct |
9 ms |
344 KB |
Output is correct |
40 |
Correct |
6 ms |
344 KB |
Output is correct |
41 |
Correct |
5 ms |
344 KB |
Output is correct |
42 |
Correct |
7 ms |
600 KB |
Output is correct |
43 |
Correct |
6 ms |
436 KB |
Output is correct |
44 |
Correct |
6 ms |
600 KB |
Output is correct |
45 |
Correct |
5 ms |
440 KB |
Output is correct |
46 |
Correct |
6 ms |
600 KB |
Output is correct |
47 |
Correct |
5 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
600 KB |
Output is correct |
49 |
Correct |
6 ms |
600 KB |
Output is correct |
50 |
Correct |
7 ms |
852 KB |
Output is correct |
51 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
52 |
Correct |
6 ms |
344 KB |
Output is correct |
53 |
Correct |
7 ms |
344 KB |
Output is correct |
54 |
Correct |
6 ms |
436 KB |
Output is correct |
55 |
Correct |
9 ms |
600 KB |
Output is correct |
56 |
Correct |
4 ms |
344 KB |
Output is correct |
57 |
Partially correct |
5 ms |
344 KB |
Output is partially correct |
58 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
59 |
Partially correct |
7 ms |
340 KB |
Output is partially correct |
60 |
Correct |
7 ms |
600 KB |
Output is correct |
61 |
Correct |
8 ms |
600 KB |
Output is correct |
62 |
Correct |
5 ms |
600 KB |
Output is correct |
63 |
Partially correct |
7 ms |
604 KB |
Output is partially correct |
64 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
65 |
Partially correct |
7 ms |
436 KB |
Output is partially correct |
66 |
Partially correct |
9 ms |
600 KB |
Output is partially correct |
67 |
Partially correct |
8 ms |
688 KB |
Output is partially correct |
68 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
69 |
Partially correct |
8 ms |
856 KB |
Output is partially correct |
70 |
Partially correct |
7 ms |
436 KB |
Output is partially correct |
71 |
Correct |
6 ms |
444 KB |
Output is correct |
72 |
Partially correct |
6 ms |
852 KB |
Output is partially correct |
73 |
Partially correct |
7 ms |
604 KB |
Output is partially correct |
74 |
Partially correct |
7 ms |
616 KB |
Output is partially correct |
75 |
Partially correct |
8 ms |
436 KB |
Output is partially correct |
76 |
Partially correct |
9 ms |
440 KB |
Output is partially correct |
77 |
Correct |
6 ms |
604 KB |
Output is correct |
78 |
Partially correct |
7 ms |
448 KB |
Output is partially correct |
79 |
Correct |
7 ms |
436 KB |
Output is correct |
80 |
Partially correct |
7 ms |
860 KB |
Output is partially correct |
81 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
82 |
Partially correct |
8 ms |
596 KB |
Output is partially correct |