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 <iostream>
#include <numeric>
#include <map>
template <class T>
using Vec = std::vector<T>;
int memo[110][110];
int getDist(const int i, const int j) {
if (i == j) return 0;
if (memo[i][j] == 0) {
memo[i][j] = memo[j][i] = getDistance(i, j);
}
return memo[i][j];
}
int hubDistance(int N, int sub) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
memo[i][j] = 0;
}
}
const auto farthest = [&](const int src) {
Vec<int> v(N);
std::iota(v.begin(), v.end(), 0);
return *std::max_element(v.begin(), v.end(), [&](const int i, const int j) {
return getDist(src, i) < getDist(src, j);
});
};
const auto X = farthest(0);
const auto Y = farthest(X);
int R = 1000005;
std::map<std::pair<int, int>, Vec<int>> map;
Vec<int> near(N);
for (int u = 0; u < N; ++u) {
const auto x = getDist(X, u);
const auto y = getDist(Y, u);
const auto z = ((x + y) - getDist(X, Y)) / 2;
const auto l = x - z;
const auto r = y - z;
map[{ l, r }].push_back(u);
R = std::min(R, std::max(l, r));
near[u] = z;
}
if (sub == 3) {
int left = 0;
Vec<char> used(N);
for (const auto &[p, v]: map) {
int max = left;
for (const auto src: v) {
if (!used[src]) {
int count = 1;
used[src] = true;
for (const auto x: v) {
if (!used[x]) {
if (getDist(src, x) < near[src] + near[x]) {
count += 1;
used[x] = true;
}
}
}
max = std::max(max, count);
}
}
left += (int) v.size();
max = std::max(max, N - left);
if (max * 2 <= N && std::max(p.first, p.second) == R) {
return R;
}
}
}
else if (sub == 4) {
int left = 0;
Vec<char> used(N);
for (const auto &[p, v]: map) {
int max = std::max(left, (int) v.size());
left += (int) v.size();
max = std::max(max, N - left);
if (max * 2 <= N && std::max(p.first, p.second) == R) {
return R;
}
}
}
return -R;
}
#ifdef LOCAL
int dist[110][110];
int getDistance(int i, int j) {
return dist[i][j];
}
int main() {
int sub, tests;
std::cin >> sub >> tests;
while (tests--) {
int N;
std::cin >> N;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
std::cin >> dist[i][j];
}
}
std::cout << hubDistance(N, sub) << '\n';
}
return 0;
}
#endif
# | 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... |