/*************************************
* 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};
bool last = false;
FOR(i, 2, N) {
if (connected_landmark(left.back(), i)) {
left.push_back(i);
last = false;
} else if (last || connected_landmark(right.back(), i)) {
right.push_back(i);
last = true;
} else {
left.insert(left.end(), right.rbegin(), right.rend());
right = vector{i};
last = false;
}
}
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;
}
}
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:91:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
91 | int m = l + r >> 1;
| ~~^~~
longesttrip.cpp:98:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
98 | int m = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
2 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 |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
340 KB |
Output is correct |
8 |
Correct |
5 ms |
596 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 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 |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
852 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
344 KB |
Output is correct |
17 |
Correct |
7 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
5 ms |
600 KB |
Output is correct |
21 |
Correct |
5 ms |
440 KB |
Output is correct |
22 |
Correct |
6 ms |
600 KB |
Output is correct |
23 |
Correct |
5 ms |
436 KB |
Output is correct |
24 |
Correct |
5 ms |
852 KB |
Output is correct |
25 |
Correct |
6 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
596 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
4 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
5 ms |
344 KB |
Output is correct |
33 |
Correct |
5 ms |
596 KB |
Output is correct |
34 |
Correct |
4 ms |
344 KB |
Output is correct |
35 |
Correct |
5 ms |
344 KB |
Output is correct |
36 |
Correct |
4 ms |
344 KB |
Output is correct |
37 |
Correct |
5 ms |
344 KB |
Output is correct |
38 |
Correct |
5 ms |
344 KB |
Output is correct |
39 |
Correct |
5 ms |
344 KB |
Output is correct |
40 |
Correct |
5 ms |
344 KB |
Output is correct |
41 |
Correct |
6 ms |
600 KB |
Output is correct |
42 |
Correct |
7 ms |
344 KB |
Output is correct |
43 |
Correct |
6 ms |
344 KB |
Output is correct |
44 |
Correct |
6 ms |
600 KB |
Output is correct |
45 |
Correct |
7 ms |
344 KB |
Output is correct |
46 |
Correct |
10 ms |
344 KB |
Output is correct |
47 |
Correct |
6 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 |
5 ms |
436 KB |
Output is correct |
51 |
Correct |
6 ms |
436 KB |
Output is correct |
52 |
Correct |
6 ms |
344 KB |
Output is correct |
53 |
Correct |
5 ms |
344 KB |
Output is correct |
54 |
Correct |
6 ms |
600 KB |
Output is correct |
55 |
Correct |
6 ms |
344 KB |
Output is correct |
56 |
Correct |
6 ms |
600 KB |
Output is correct |
57 |
Correct |
5 ms |
600 KB |
Output is correct |
58 |
Correct |
6 ms |
444 KB |
Output is correct |
59 |
Correct |
6 ms |
600 KB |
Output is correct |
60 |
Correct |
9 ms |
600 KB |
Output is correct |
61 |
Correct |
7 ms |
600 KB |
Output is correct |
62 |
Correct |
7 ms |
444 KB |
Output is correct |
63 |
Correct |
8 ms |
856 KB |
Output is correct |
64 |
Correct |
7 ms |
856 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 |
4 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 |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
596 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 |
436 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
6 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
6 ms |
440 KB |
Output is correct |
20 |
Correct |
5 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 |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
344 KB |
Output is correct |
25 |
Correct |
5 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
344 KB |
Output is correct |
30 |
Correct |
5 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
608 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
8 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
7 ms |
344 KB |
Output is correct |
36 |
Correct |
9 ms |
344 KB |
Output is correct |
37 |
Correct |
5 ms |
440 KB |
Output is correct |
38 |
Correct |
6 ms |
432 KB |
Output is correct |
39 |
Correct |
7 ms |
344 KB |
Output is correct |
40 |
Correct |
5 ms |
344 KB |
Output is correct |
41 |
Correct |
5 ms |
344 KB |
Output is correct |
42 |
Correct |
5 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
440 KB |
Output is correct |
44 |
Correct |
5 ms |
600 KB |
Output is correct |
45 |
Correct |
6 ms |
448 KB |
Output is correct |
46 |
Correct |
7 ms |
604 KB |
Output is correct |
47 |
Correct |
5 ms |
344 KB |
Output is correct |
48 |
Correct |
5 ms |
344 KB |
Output is correct |
49 |
Correct |
6 ms |
600 KB |
Output is correct |
50 |
Correct |
6 ms |
600 KB |
Output is correct |
51 |
Correct |
5 ms |
344 KB |
Output is correct |
52 |
Correct |
6 ms |
344 KB |
Output is correct |
53 |
Correct |
5 ms |
600 KB |
Output is correct |
54 |
Correct |
5 ms |
344 KB |
Output is correct |
55 |
Correct |
6 ms |
344 KB |
Output is correct |
56 |
Correct |
5 ms |
344 KB |
Output is correct |
57 |
Correct |
5 ms |
344 KB |
Output is correct |
58 |
Correct |
6 ms |
344 KB |
Output is correct |
59 |
Correct |
6 ms |
344 KB |
Output is correct |
60 |
Correct |
6 ms |
344 KB |
Output is correct |
61 |
Correct |
6 ms |
344 KB |
Output is correct |
62 |
Correct |
5 ms |
440 KB |
Output is correct |
63 |
Correct |
5 ms |
444 KB |
Output is correct |
64 |
Correct |
5 ms |
444 KB |
Output is correct |
65 |
Correct |
6 ms |
856 KB |
Output is correct |
66 |
Correct |
8 ms |
856 KB |
Output is correct |
67 |
Correct |
7 ms |
696 KB |
Output is correct |
68 |
Correct |
7 ms |
432 KB |
Output is correct |
69 |
Correct |
9 ms |
600 KB |
Output is correct |
70 |
Correct |
8 ms |
436 KB |
Output is correct |
71 |
Correct |
7 ms |
696 KB |
Output is correct |
72 |
Correct |
6 ms |
604 KB |
Output is correct |
73 |
Correct |
6 ms |
600 KB |
Output is correct |
74 |
Correct |
6 ms |
692 KB |
Output is correct |
75 |
Correct |
6 ms |
700 KB |
Output is correct |
76 |
Correct |
7 ms |
600 KB |
Output is correct |
77 |
Correct |
5 ms |
444 KB |
Output is correct |
78 |
Correct |
6 ms |
440 KB |
Output is correct |
79 |
Correct |
6 ms |
440 KB |
Output is correct |
80 |
Correct |
6 ms |
452 KB |
Output is correct |
81 |
Correct |
6 ms |
440 KB |
Output is correct |
82 |
Correct |
6 ms |
608 KB |
Output is correct |