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 = 310;
int dist[MAXN][MAXN];
int marc[MAXN][MAXN];
int morto[MAXN];
int p1, p2, n;
int cnt;
int getD(int i, int j){
if(marc[i][j]) return dist[i][j];
marc[i][j] = marc[j][i] = 1;
if(i == j) return dist[i][j] = dist[j][i] = 0;
cnt++;
return dist[i][j] = dist[j][i] = getDistance(i, j);
}
int getSize(vector<int> grp, vector<int> test, int goal){
int cnt1 = 1, cnt2 = 0;
while(true){
bool flag = true;
int x;
int id;
for(int i = 0; i < grp.size(); i++){
for(int j = 0; j < test.size(); j++){
if(morto[test[j]]) continue;
if(!marc[grp[i]][test[j]]) continue;
flag = false;
x = (getD(p1, grp[i]) + getD(p1, test[j]) - getD(grp[i], test[j])) / 2;
id = j;
break;
}
if(!flag) break;
}
if(flag){
for(int j = 0; j < test.size(); j++){
if(morto[test[j]]) continue;
flag = false;
x = (getD(p1, grp[0]) + getD(p1, test[j]) - getD(grp[0], test[j])) / 2;
id = j;
break;
}
}
if(flag) break;
if(x > goal) cnt1++, grp.push_back(test[id]);
else cnt2++;
morto[test[id]] = 1;
if(cnt1 > n / 2 || cnt2 > n / 2) break;
}
return (cnt1 > n / 2) ? -1 : 1;
}
int hubDistance(int N, int sub) {
n = N;
p1 = 0, p2 = 0;
int maxi = 0;
memset(dist, 0, sizeof(dist));
memset(marc, 0, sizeof(marc));
memset(morto, 0, sizeof(morto));
cnt = 0;
for(int i = 0; i < N; i++){
int aux = getD(0, i);
if(aux > maxi) p1 = i, maxi = aux;
}
maxi = 0;
for(int i = 0; i < N; i++)
if(getD(p1, i) > maxi) maxi = getD(p1, i), p2 = i;
int diam = getD(p1, p2);
int cruz0 = (diam + getD(p1, 0) - getD(p2, 0)) / 2;
int ans = max(cruz0, diam - cruz0);
for(int i = 0; i < N; i++){
int x = (getD(p1, 0) + getD(p1, i) - getD(0, 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 = (getD(p1, 0) + getD(p1, i) - getD(0, 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 = getD(p1, pilha.top());
int x = (aux + getD(p1, lMid[i]) - getD(pilha.top(), lMid[i])) / 2;
if(x == min(ans, diam - ans)) pilha.pop();
else pilha.push(lMid[i]);
}
}
if(pilha.empty()) return ans;
vector<int> grp;
while(!pilha.empty()){
int k = pilha.top();
grp.push_back(k);
morto[k] = 1;
pilha.pop();
}
return getSize(grp, lMid, min(ans, diam - ans)) * 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 = getD(p1, pilha.top());
int x = (aux + getD(p1, rMid[i]) - getD(pilha.top(), rMid[i])) / 2;
if(x == max(ans, diam - ans)) pilha.pop();
else pilha.push(rMid[i]);
}
}
if(pilha.empty()) return ans;
vector<int> grp;
while(!pilha.empty()){
int k = pilha.top();
grp.push_back(k);
morto[k] = 1;
pilha.pop();
}
return getSize(grp, rMid, max(ans, diam - ans)) * ans;
}
return ans;
}
Compilation message (stderr)
towns.cpp: In function 'int getSize(std::vector<int>, std::vector<int>, int)':
towns.cpp:27:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i = 0; i < grp.size(); i++){
| ~~^~~~~~~~~~~~
towns.cpp:28:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int j = 0; j < test.size(); j++){
| ~~^~~~~~~~~~~~~
towns.cpp:39:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int j = 0; j < test.size(); j++){
| ~~^~~~~~~~~~~~~
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:92:52: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
92 | if(lSize > N / 2 || rSize > N / 2 || lMid.size() > N / 2 || rMid.size() > N / 2) return -ans;
| ~~~~~~~~~~~~^~~~~~~
towns.cpp:92:75: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
92 | if(lSize > N / 2 || rSize > N / 2 || lMid.size() > N / 2 || rMid.size() > N / 2) return -ans;
| ~~~~~~~~~~~~^~~~~~~
towns.cpp:97:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
97 | else if(lMid.size() > N / 2){
| ~~~~~~~~~~~~^~~~~~~
towns.cpp:99:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for(int i = 0; i < lMid.size(); i++){
| ~~^~~~~~~~~~~~~
towns.cpp:118:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
118 | else if(rMid.size() > N / 2){
| ~~~~~~~~~~~~^~~~~~~
towns.cpp:120:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | 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... |