This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 09.11.2023 14:50:12 *
*************************************/
#include "fun.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 << '}'; }
// end of template
vector <int> createFunTour(int n, int q) {
int min_sz = n, cen = 0;
FOR(u, 1, n) {
int sz_u = attractionsBehind(0, u);
if (sz_u > n / 2 && sz_u < min_sz) min_sz = sz_u, cen = u;
}
vector <int> dist(n), child;
REP(i, n) if (i != cen) {
dist[i] = hoursRequired(cen, i);
if (dist[i] == 1) child.push_back(i);
}
vector <int> comps(n, child.size() - 1);
REP(i, child.size() - 1) {
comps[child[i]] = i;
REP(u, n) if (u != cen && comps[u] == child.size() - 1 && hoursRequired(u, child[i]) + 1 == dist[u]) comps[u] = i;
}
vector <int> sz(3);
REP(i, n) if (i != cen) ++sz[comps[i]];
int mi = min_element(ALL(sz)) - sz.begin();
comps[cen] = mi;
++sz[mi];
vector <vector <int>> List(3);
REP(i, n) List[comps[i]].push_back(i);
REP(i, 3) sort(ALL(List[i]), [&] (int u, int v) { return dist[u] < dist[v]; });
int last = -1;
vector <int> res;
FORDE(sum, n, 1) {
bool ok = false;
REP(i, 3) if (sz[i] + sz[i] == sum) {
vector <int> new_list;
int f = 1;
if (last >= 0 && last != i) {
int k = 3 - i - last;
if (dist[List[last].back()] >= dist[List[3 - i - last].back()]) f = 0;
}
REP(j, 3) if (j != i) new_list.insert(new_list.end(), ALL(List[j]));
sort(ALL(new_list), [&] (int u, int v) { return dist[u] < dist[v]; });
REPD(t, sz[i]) {
if (f) {
res.push_back(new_list[t]);
res.push_back(List[i][t]);
} else {
res.push_back(List[i][t]);
res.push_back(new_list[t]);
}
}
ok = true;
break;
}
if (ok) break;
int ma = -1;
REP(i, 3) if (i != last && !List[i].empty() && (ma < 0 || dist[List[ma].back()] < dist[List[i].back()])) ma = i;
res.push_back(List[ma].back());
List[ma].pop_back();
last = ma;
--sz[ma];
}
return res;
}
Compilation message (stderr)
fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:60:38: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | REP(u, n) if (u != cen && comps[u] == child.size() - 1 && hoursRequired(u, child[i]) + 1 == dist[u]) comps[u] = i;
fun.cpp:78:9: warning: unused variable 'k' [-Wunused-variable]
78 | int k = 3 - i - last;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |