31642023-02-21 11:53:51TuruTamasHírvivők csoportosítása (40)cpp17Accepted 40/4017ms4344 KiB
#include <bits/stdc++.h>
using namespace std;

int N, M, H, K;
vector<bool> been;
vector<pair<int, int>> hirek;

void dfs(int& d, int loc) {
    been[loc] = true;
    for (size_t i = 0; i < hirek.size(); i++)
    {
        if (i == loc || been[i]) continue;
        if (abs(hirek[i].first - hirek[loc].first) + abs(hirek[i].second - hirek[loc].second) <= d) dfs(d, i);
    }
}

void getGroups(int& csoportok, int n) {
    csoportok = 0;
    been.assign(H, false);
    for (size_t i = 0; i < H; i++)
    {
        if (been[i]) continue;
        csoportok++;
        dfs(n, i);
    }
}

int main() {
    cin.tie(0); ios::sync_with_stdio(0);
    cin >> N >> M >> H >> K;
    int x, y;
    for (size_t i = 0; i < H; i++)
    {
        cin >> x >> y;
        hirek.push_back({x, y});
    }
    int csoportok, minn = 0, maxn = N+M, n = log2(N+M);
    int lastn = n;
    while (true) {
        int csoportok;
        getGroups(csoportok, n);
        if (csoportok > K) {
            minn = n;
            n = (n+maxn)/2;
        }
        else if (csoportok <= K) {
            maxn = n;
            n = (n+minn)/2;
        }
        if (n == lastn) break;
        lastn = n;
    }
    if (csoportok <= K) {
        cout << n;
    }
    else {
        while (csoportok > K) {
            n++;
            getGroups(csoportok, n);
        }
        cout << n;
    }
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1764 KiB
2Accepted0/014ms2160 KiB
3Accepted1/13ms2480 KiB
4Accepted1/13ms2552 KiB
5Accepted1/14ms2664 KiB
6Accepted1/14ms2872 KiB
7Accepted1/13ms2948 KiB
8Accepted1/13ms3076 KiB
9Accepted1/13ms3024 KiB
10Accepted1/16ms3260 KiB
11Accepted1/14ms3472 KiB
12Accepted1/14ms3440 KiB
13Accepted3/316ms3468 KiB
14Accepted3/316ms3784 KiB
15Accepted3/317ms4008 KiB
16Accepted3/317ms4208 KiB
17Accepted3/317ms4152 KiB
18Accepted3/316ms4276 KiB
19Accepted3/317ms4036 KiB
20Accepted3/317ms4288 KiB
21Accepted3/316ms4344 KiB
22Accepted3/316ms4240 KiB