#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
int hubDistance(int N, int sub) {
pair<int, int > best = make_pair(-1,-1);
//first is dist, second is ind
for(int i = 1; i<N; i++){
pair<int, int> now = make_pair(getDistance(0,i),i);
best = max(best,now);
}
int first = best.second;
best = make_pair(-1,-1);
int A[N];
int B[N];
for(int i = 0; i<N; i++){
if(i==first){
continue;
}
pair<int, int> now = make_pair(getDistance(first,i),i);
A[i] = now.first;
best = max(best,now);
}
int second = best.second;
int diameter = A[second];
int inf = 100000000;
int ans = inf;
vector<vector<int> > li;
int point = 0;
int most = N/2;
bool balanced = true;
map<pair<int, int>, int> m;
vector<int> ali;
int x[N];
for(int i = 0; i<N; i++){
if(i==first || i==second){
continue;
}
B[i] = getDistance(i,second);
x[i] = (A[i]+B[i]-diameter)/2;
int a = A[i]-x[i];
int b = B[i]-x[i];
pair<int, int> comb = make_pair(a,b);
bool newer = false;
if(m.find(comb)==m.end()){
newer = true;
m[comb] = point++;
vector<int> xx;
li.push_back(xx);
}
li[m[comb]].push_back(i);
if(max(a,b)<ans){
ans = max(a,b);
ali.clear();
ali.push_back(point-1);
}
else if(max(a,b)==ans && newer){
ali.push_back(point-1);
}
}
for(int i = 0; i<li.size(); i++){
if(li[i].size()>most){
bool ishub = false;
for(int j = 0; j<ali.size(); j++){
ishub |= ali[j]==i;
}
if(!ishub){
balanced =false;
break;
}
int cur = li[i][0];
int score = 1;
for(int j = 1; j<li[i].size(); j++){
if(score==0){
cur = li[i][j];
score = 1;
}
else if(getDistance(li[i][j],cur)==x[li[i][j]]+x[cur]){
score--;
}
else{
score++;
}
}
if(score==0){
break;
}
int cnt = 1;
for(int j = 0; j<li[i].size(); j++){
if(li[i][j]==cur){
continue;
}
if(getDistance(li[i][j],cur)<x[li[i][j]]+x[cur]){
cnt++;
}
}
if(cnt > most){
balanced = false;
}
break;
}
}
if(!balanced){
ans = -ans;
}
return ans;
}
Compilation message
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:60:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i<li.size(); i++){
~^~~~~~~~~~
towns.cpp:61:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(li[i].size()>most){
~~~~~~~~~~~~^~~~~
towns.cpp:63:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j<ali.size(); j++){
~^~~~~~~~~~~
towns.cpp:72:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 1; j<li[i].size(); j++){
~^~~~~~~~~~~~~
towns.cpp:88:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j<li[i].size(); j++){
~^~~~~~~~~~~~~
towns.cpp:4:28: warning: unused parameter 'sub' [-Wunused-parameter]
int hubDistance(int N, int sub) {
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
376 KB |
Output is correct |
2 |
Correct |
17 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
560 KB |
Output is correct |
4 |
Correct |
21 ms |
672 KB |
Output is correct |
5 |
Correct |
21 ms |
672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
692 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |