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<vector>
#include<algorithm>
#include<utility>
#include<map>
using namespace std;
int hubDistance(int N, int sub) {
int a = -1, b = -1;
int maxi = 0, n = N;
for(int i = 1; i < n; i++){
int len = getDistance(0, i);
if(len > maxi){
maxi = len;
a = i;
}
}
vector<int> dista(n), distb(n);
maxi = 0;
for(int i = 0; i < n; i++){
dista[i] = getDistance(a, i);
if(dista[i] > maxi){
maxi = dista[i];
b = i;
}
}
for(int i = 0; i < n; i++){
distb[i] = getDistance(b, i);
}
int d = dista[b];
int ans = 1e9;
for(int i = 0; i < n; i++){
int da = (d + dista[i] + distb[i]) / 2 - distb[i], db = (d + dista[i] + distb[i]) / 2 - dista[i];
ans = min(ans, max(da, db));
}
if(sub == 4){
map<int, int> m;
for(int i = 0; i < n; i++){
m[(d + distb[i] + dista[i]) / 2 - distb[i]]++;
}
int sum = 0;
bool flag = 0;
for(auto &[len, val]: m){
if(max(len, d - len) == ans){
if(max(val, max(sum, n - sum - val)) <= n / 2) flag = 1;
}
sum += val;
}
if(flag) return ans;
else return -ans;
}
return ans;
}
// g++ -std=c++17 -Wall -Wextra -fsanitize=undefined -fsanitize=address -Wshadow -o run towns.cpp grader.cpp
# | 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... |