제출 #368607

#제출 시각아이디문제언어결과실행 시간메모리
368607KoDTowns (IOI15_towns)C++17
48 / 100
29 ms512 KiB
#include <bits/stdc++.h>
#include "towns.h"

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;
    }
    Vec<Vec<int>> cand;
    {
        int left = 0;
        for (const auto &[p, v]: map) {
            if (std::max(left, N - (int) v.size() - left) * 2 <= N && std::max(p.first, p.second) == R) {
                cand.push_back(v);
            }
            left += (int) v.size(); 
        }
    }
    for (const auto &v: cand) {
        if ((int) v.size() * 2 <= N) {
            return R;
        }
    }
    if (cand.empty()) {
        return -R;
    }
    assert(cand.size() == 1);
    if (sub == 3) {
        Vec<char> used(N);
        for (const auto &v: cand) {
            int max = 0;
            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);
                }
            }
            if (max * 2 <= N) {
                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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...