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;
const int MAXN = 1e6;
int dist[3][MAXN];
int hubDistance(int N, int sub) {
int p1 = 0, p2 = 0;
int maxi = 0;
for(int i = 0; i < N; i++){
int aux = getDistance(0, i);
if(aux > maxi) p1 = i, maxi = aux;
}
maxi = 0;
for(int i = 0; i < N; i++){
dist[0][i] = getDistance(p1, i);
if(dist[0][i] > maxi) maxi = dist[0][i], p2 = i;
}
for(int i = 0; i < N; i++)
dist[1][i] = getDistance(p2, i);
int diam = dist[0][p2];
int ans = MAXN;
for(int i = 0; i < N; i++){
int x = (diam + dist[0][i] - dist[1][i]) / 2;
ans = min(ans, max(x, diam - x));
}
if(sub <= 2) return ans;
int lSize = 0, rSize = 0;
vector<int> lMid, rMid;
for(int i = 0; i < N; i++){
int x = (diam + dist[0][i] - dist[1][i]) / 2;
if(x < min(ans, diam - ans)) lSize++;
else if(x == min(ans, diam - ans)) lMid.push_back(i);
else if(x == max(ans, diam - ans)) rMid.push_back(i);
else rSize++;
}
if(sub <= 4){
if(lSize > N / 2 || rSize > N / 2 || lMid.size() > N / 2 || rMid.size() > N / 2) return -ans;
return ans;
}
if(lSize > N / 2 || rSize > N / 2) return -ans;
else if(lMid.size() > N / 2){
stack<int> pilha;
for(int i = 0; i < lMid.size(); i++){
if(pilha.empty()) pilha.push(lMid[i]);
else{
int aux = dist[0][pilha.top()];
int x = (aux + dist[0][lMid[i]] - getDistance(pilha.top(), lMid[i])) / 2;
if(x == min(ans, diam - ans)) pilha.pop();
else pilha.push(lMid[i]);
}
}
if(pilha.empty()) return ans;
int k = pilha.top();
int cnt = 0;
for(int i = 0; i < lMid.size(); i++){
int aux = dist[0][k];
int x = (aux + dist[0][lMid[i]] - getDistance(k, lMid[i])) / 2;
if(x > min(ans, diam - ans)) cnt++;
}
if(cnt > N / 2) return -ans;
return ans;
}
else if(rMid.size() > N / 2){
stack<int> pilha;
for(int i = 0; i < rMid.size(); i++){
if(pilha.empty()) pilha.push(rMid[i]);
else{
int aux = dist[0][pilha.top()];
int x = (aux + dist[0][rMid[i]] - getDistance(pilha.top(), rMid[i])) / 2;
if(x == max(ans, diam - ans)) pilha.pop();
else pilha.push(rMid[i]);
}
}
if(pilha.empty()) return ans;
int k = pilha.top();
int cnt = 0;
for(int i = 0; i < rMid.size(); i++){
int aux = dist[0][k];
int x = (aux + dist[0][rMid[i]] - getDistance(k, rMid[i])) / 2;
if(x > max(ans, diam - ans)) cnt++;
}
if(cnt > N / 2) return -ans;
return ans;
}
return ans;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:43:52: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
43 | if(lSize > N / 2 || rSize > N / 2 || lMid.size() > N / 2 || rMid.size() > N / 2) return -ans;
| ~~~~~~~~~~~~^~~~~~~
towns.cpp:43:75: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
43 | if(lSize > N / 2 || rSize > N / 2 || lMid.size() > N / 2 || rMid.size() > N / 2) return -ans;
| ~~~~~~~~~~~~^~~~~~~
towns.cpp:48:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
48 | else if(lMid.size() > N / 2){
| ~~~~~~~~~~~~^~~~~~~
towns.cpp:50:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i = 0; i < lMid.size(); i++){
| ~~^~~~~~~~~~~~~
towns.cpp:62:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int i = 0; i < lMid.size(); i++){
| ~~^~~~~~~~~~~~~
towns.cpp:70:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
70 | else if(rMid.size() > N / 2){
| ~~~~~~~~~~~~^~~~~~~
towns.cpp:72:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 0; i < rMid.size(); i++){
| ~~^~~~~~~~~~~~~
towns.cpp:84:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 0; i < rMid.size(); i++){
| ~~^~~~~~~~~~~~~
# | 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... |