This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define f first
#define s second
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#endif // BALBIT
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int )((x).size())
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
map<pii, int> mp;
int getd(int a, int b){
if (a==b) return 0;
if (a>b) swap(a,b);
if (!mp.count({a,b})) mp[{a,b}] = getDistance(a,b);
return mp[{a,b}];
}
int hubDistance(int n, int sub) {
mp.clear();
int p1=-1, p2=-1;
vector<int> C(n);
{
int far = 0;
REP(i,n) {
if (i != 0) {
C[i] = getd(0,i);
if (getd(0,i) > far) {
far = getd(0,i);
p1 = i;
}
}
}
}
vector<int> A(n);
vector<int> B(n);
{
int far = 0;
REP(i,n) {
if (i!=p1) {
A[i] = getd(p1, i);
if (getd(p1,i) > far) {
far = getd(p1,i);
p2 = i;
}
}
}
}
assert(p1 != -1 && p2 != -1);
bug(p1, p2);
int dia = A[p2];
int P = (C[p1] + C[p2] - dia) / 2;
int R = 2000000000;
bug(P);
int L = C[p1] - P;
map<int, vector<int> > under;
int cent = -1;
int ncent = -1;
REP(i,n) {
// B[i] = getd(p2,i);
if (A[i] - C[i] == L-P) {
bug("hI",i);
under[C[p1] - P].pb(i);
continue;
// just pretend it's under that
}
B[i] = C[p2] + C[i] - P - P;
int pp = (A[i] + B[i] - dia) / 2;
under[A[i] - pp].pb(i);
}
int who = -1;
int nstuff = 0;
for (auto &ele : under) {
int hi = max(ele.f, dia-ele.f);
// assert(hi % 2 == 0);
if (hi < R) {
R = hi;
}
if (hi == R) {
if (nstuff <= n/2 && (n - nstuff - SZ(ele.s)) <= n/2) {
who = ele.f;
}
}
nstuff += SZ(ele.s);
}
bug(dia, R);
if (who == -1) return -R;
if (SZ(under[who]) <= n/2) return R;
return R;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:78:9: warning: unused variable 'cent' [-Wunused-variable]
78 | int cent = -1;
| ^~~~
towns.cpp:79:9: warning: unused variable 'ncent' [-Wunused-variable]
79 | int ncent = -1;
| ^~~~~
towns.cpp:40:28: warning: unused parameter 'sub' [-Wunused-parameter]
40 | 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... |