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;
int hubDistance(int N, int sub) {
map<int,int> d[N];
auto get = [&](int a,int b){
if(a > b)
swap(a,b);
if(d[a].count(b))
return d[a][b];
if(a == b)
return 0;
return d[a][b] = getDistance(a,b);
};
int v = 0;
for(int i = 0;i<N;i++){
if(get(0,i) > get(0,v)){
v = i;
}
}
int x = 0;
for(int i = 0;i<N;i++){
if(get(v,i) > get(v,x)){
x = i;
}
}
int len = get(v,x);
// cout << v << ' ' << x << ' ' << len << endl;
map<int,vector<int>> mp;
for(int i = 0;i<N;i++){
int val = (get(0,i) + get(0,v) - get(v,i))/2;
mp[val].push_back(i);
}
int val1 = 0;
int val2 = 2e9;
int ans;
for(auto u:mp){
int tmp = get(0,v) - u.first;
// cout << tmp << endl;
if(tmp <= len/2){
val1 = max(val1,tmp);
}
if((len + 1)/2 <= tmp){
val2 = min(val2,tmp);
}
}
// cout << val1 << ' ' << val2 << endl;
ans = len - val1;
if(val2 != len - val1){
if(val2 < len - val1){
ans = val2;
val1 = -1;
}
else val2 = -1;
}
int cnt = 0;
// cout << len << endl;
for(auto it = mp.rbegin();it != mp.rend();it++){
auto u = *it;
int tmp = get(0,v) - u.first;
// cout << tmp << endl;
for(auto c:u.second){
// cout << c << ' ';
}
// cout << endl;
if(tmp == val1){
if(cnt > N/2 || (N-cnt-u.second.size()) > N/2){
val1 = -1;
}
}
if(tmp == val2){
if(cnt > N/2){
val2 = -1;
}
}
cnt += u.second.size();
}
if(val1 == -1 && val2 == -1)
return -ans;
vector<int> a;
if(val1 != -1)
a = mp[-(val1 - get(0,v))];
else{
val1 = val2;
for(auto u:mp){
int tmp = get(0,v) - u.first;
if(tmp < val2)continue;
for(auto c:u.second){
a.push_back(c);
}
}
}
// cout << val1 << endl;
vector<int> v1,v2;
for(auto u:a){
// cout << u << ' ';
if(v1.empty() || get(u,v1.back()) == get(v,u) + get(v,v1.back()) - 2 * val1){
v1.push_back(u);
if(v2.size()){
v1.push_back(v2.back());
v2.pop_back();
}
}
else v2.push_back(u);
}
// cout << endl;
// cout << a.size() << endl;
if(v2.empty())
return ans;
cnt = v2.size();
int tmp = v2[0];
while(v1.size()){
int u = v1.back();
if(get(u,tmp) != get(v,u) + get(v,tmp) - 2 * val1){
cnt++;
v1.pop_back();
if(v1.size())
v1.pop_back();
}
else{
v1.pop_back();
if(v2.empty())
break;
v2.pop_back();
}
}
// cout << cnt << endl;
if(cnt <= N/2)
return ans;
return -ans;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:62:12: warning: unused variable 'c' [-Wunused-variable]
62 | for(auto c:u.second){
| ^
towns.cpp:67:44: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
67 | if(cnt > N/2 || (N-cnt-u.second.size()) > N/2){
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
towns.cpp:76:24: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
76 | cnt += u.second.size();
| ^
towns.cpp:110:15: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
110 | cnt = v2.size();
| ~~~~~~~^~
towns.cpp:4:28: warning: unused parameter 'sub' [-Wunused-parameter]
4 | 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... |