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 SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(),(v).end()
#define one first
#define two second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<double, double> pd;
typedef pair<int, int> pi;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll; typedef pair<ll, pi> plp;
typedef tuple<int, int, int> ti; typedef tuple<ll, int, int> tli;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll * INF * INF * 2;
map<pi, int> Mp;
int getD(int i, int j) {
if(Mp.count(pi(i, j)) == 0) Mp[pi(i, j)] = Mp[pi(j, i)] = getDistance(i, j);
return Mp[pi(i, j)];
}
int N, Sub;
int max0D, f, maxfD, s;
bool isDiff(int a, int b, int dis) {
return (getD(f, a) + getD(f, b) - 2 * dis) == getD(a, b);
}
bool haveMajor(int dis, vector<int> candi) {
if(SZ(candi) == 0) return false;
vector<int> minor;
vector<pi> rep;
int nowX = -1, nowL = 0, maxL = 0;
for(int x : candi) {
if(nowX == -1) {
nowX = x; nowL = maxL = 0;
}else{
if(!isDiff(nowX, x, dis)) nowL++, maxL++;
else{
nowL--;
minor.push_back(x);
if(nowL == 0) {
rep.push_back(pi(nowX, maxL));
nowX = -1;
}
}
}
}
if(nowX != -1) rep.push_back(pi(nowX, maxL));
int mj = rep.back().one, cnt = 0;
for(pi e : rep) if(!isDiff(mj, e.one, dis)) cnt += e.two;
for(int x : minor) if(!isDiff(mj, x, dis)) cnt++;
return cnt >= N/2;
}
int hubDistance(int n, int sub) {
Mp.clear();
N = n; Sub = sub;
max0D = -1, f = -1;
for(int i=0; i<N; i++) {
if(i == 0) continue;
int d = getD(0, i);
if(max0D < d) max0D = d, f = i;
}
maxfD = -1, s = -1;
for(int i=0; i<N; i++) {
if(i == f) continue;
int d = getD(f, i);
if(maxfD < d) maxfD = d, s = i;
}
int R = INF;
int from0 = (getD(0, f) + getD(0, s) - getD(s, f)) / 2;
vector<pi> list;
for(int i=0; i<N; i++) {
int dis = (getD(0, i) + getD(0, f) - getD(i, f)) / 2;
int r = -1;
if(dis >= from0) {
r = max0D - dis;
R = min(R, max(r, maxfD - r));
}else{
r = max0D - from0;
}
list.push_back(pi(r, i));
}
sort(ALL(list));
int lim = N/2;
for(int i=0, j=0; i<N; i=j) {
vector<int> candi;
while(j<N && list[i].one == list[j].one) candi.push_back(list[j++].two);
if(i <= lim && N-j <= lim && j-i <= lim) return R;
if(i <= lim && N-j <= lim && j-i > lim) {
if(!haveMajor(list[i].one, candi)) return R;
}
}
return -R;
}
# | 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... |