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: 08.03.2023 17:37:48 *
*************************************/
#include <bits/stdc++.h>
#include "towns.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--; )
#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 T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> scan_op(pair <U, V>) { return in >> u.fi >> u.se; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
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 << '}'; }
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 A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rand(long long l, long long h) { return uniform_int_distribution <long long> (l, h) (rng); }
// end of template
int hubDistance(int N, int sub) {
vector <int> dist0(N), distu(N);
FOR(i, 1, N) dist0[i] = getDistance(0, i);
int u = max_element(ALL(dist0)) - dist0.begin();
REP(i, N) if (i != u) distu[i] = getDistance(u, i);
int diameter = *max_element(ALL(distu));
int res = diameter;
map <int, vector <int>> List;
REP(i, N) {
int dd = (distu[0] + distu[i] - dist0[i]) / 2;
minimize(res, max(dd, diameter - dd));
List[dd].push_back(i);
}
int cnt_left = 0;
for (auto [dd, l]: List) {
if (max(dd, diameter - dd) == res) {
int cnt_right = N - cnt_left - (int) l.size();
if (cnt_left <= N / 2 && cnt_right <= N / 2) {
vector <vector <int>> alive, dead;
vector <int> candidate;
for (int u: l) alive.push_back(vector<int>{u});
auto checkComp = [&] (int x, int y) {
return distu[x] + distu[y] - getDistance(x, y) != 2 * dd;
};
while (!alive.empty()) {
vector <vector <int>> nxt;
for (int i = 0; i < (int) alive.size(); i += 2) {
if (i + 1 == (int) alive.size()) {
if (!candidate.empty()) dead.push_back(candidate);
candidate = alive[i];
} else {
if (checkComp(alive[i][0], alive[i + 1][0])) {
nxt.push_back(alive[i]);
nxt.back().insert(nxt.back().end(), ALL(alive[i + 1]));
} else {
dead.push_back(alive[i]);
dead.push_back(alive[i + 1]);
}
}
}
alive = move(nxt);
}
if (candidate.empty()) return res;
int cnt = candidate.size();
for (auto v: dead) if (checkComp(candidate[0], v[0])) cnt += v.size();
if (cnt <= N / 2) return res;
}
}
cnt_left += l.size();
}
return -res;
}
#ifdef LOCAL
#include "graderlib.c"
int main() {
FILE *f;
f = freopen("towns.in","r",stdin);
assert(f != NULL);
f = freopen("towns.out","w",stdout);
assert(f != NULL);
int ncase, R, N;
int subtask;
int ret;
ret = scanf("%d%d",&subtask,&ncase);
assert(ret == 2);
for (int i = 0; i < ncase; i++) {
ret = scanf("%d",&N);
assert(ret == 1);
_ini_query(N,subtask);
R=hubDistance(N,subtask);
printf("%d\n",R);
}
return 0;
}
#endif
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:54:34: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
54 | int u = max_element(ALL(dist0)) - dist0.begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
towns.cpp:71:14: warning: declaration of 'u' shadows a previous local [-Wshadow]
71 | for (int u: l) alive.push_back(vector<int>{u});
| ^
towns.cpp:54:6: note: shadowed declaration is here
54 | int u = max_element(ALL(dist0)) - dist0.begin();
| ^
towns.cpp:94:29: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
94 | int cnt = candidate.size();
| ~~~~~~~~~~~~~~^~
towns.cpp:95:73: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
95 | for (auto v: dead) if (checkComp(candidate[0], v[0])) cnt += v.size();
| ^
towns.cpp:99:22: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
99 | cnt_left += l.size();
| ^
towns.cpp:51:28: warning: unused parameter 'sub' [-Wunused-parameter]
51 | int hubDistance(int N, int sub) {
| ~~~~^~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |