# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
640802 | piOOE | Towns (IOI15_towns) | C++17 | 15 ms | 360 KiB |
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 <bits/stdc++.h>
#include "towns.h"
using namespace std;
int hubDistance(int n, int sub) {
vector<int> dist0(n);
for (int i = 1; i < n; ++i) {
dist0[i] = getDistance(0, i);
}
int a = max_element(dist0.begin(), dist0.end()) - dist0.begin();
vector<int> dist(n);
dist[0] = dist0[a];
for (int i = 1; i < n; ++i) {
dist[i] = i == a ? 0 : getDistance(i, a);
}
int b = max_element(dist.begin(), dist.end()) - dist.begin();
vector<int> pos(n);
pos[0] = (dist0[a] - dist0[b] + dist[b]) >> 1;
for (int i = 1; i < n; ++i) {
pos[i] = min(pos[0], (dist0[a] - dist0[i] + dist[i]) >> 1);
}
int ans = numeric_limits<int>::max();
map<int, vector<int>> mp;
for (int i = 0; i < n; ++i) {
ans = min(ans, max(pos[i], dist[b] - pos[i]));
mp[pos[i]].push_back(i);
}
int L = 0, R = n;
for (auto [len, adj]: mp) {
R -= adj.size();
if (max(len, dist[b] - len) == ans && max(L, R) <= n / 2) {
if (adj.size() <= n / 2) {
return ans;
}
vector<int> p(n);
int bal = 0, x;
for (int y: adj) {
if (bal == 0) {
x = y;
}
if (x == y || getDistance(x, y) < dist[x] + dist[y] - 2 * len) {
p[y] = x, ++bal;
} else {
p[y] = y, --bal;
}
}
int last = -1, cnt = 0;
for (int y: adj) {
if (p[y] == y && getDistance(x, y) < dist[x] + dist[y] - 2 * len) {
last = y;
}
cnt += p[y] == last;
}
if (cnt <= n / 2) {
return ans;
} else {
return -ans;
}
}
L += adj.size();
}
return -ans;
}
Compilation message (stderr)
# | 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... |