31622023-02-21 11:38:00TuruTamasHírvivők csoportosítása (40)cpp17Accepted 40/4020ms4628 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 >> 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/03ms2024 KiB
2Accepted0/016ms2436 KiB
3Accepted1/13ms2512 KiB
4Accepted1/13ms2720 KiB
5Accepted1/14ms2824 KiB
6Accepted1/16ms2824 KiB
7Accepted1/13ms2928 KiB
8Accepted1/13ms3100 KiB
9Accepted1/14ms3064 KiB
10Accepted1/16ms3192 KiB
11Accepted1/16ms3328 KiB
12Accepted1/16ms3412 KiB
13Accepted3/319ms3800 KiB
14Accepted3/318ms3896 KiB
15Accepted3/319ms4164 KiB
16Accepted3/320ms4280 KiB
17Accepted3/320ms4496 KiB
18Accepted3/319ms4476 KiB
19Accepted3/319ms4480 KiB
20Accepted3/319ms4476 KiB
21Accepted3/318ms4628 KiB
22Accepted3/318ms4476 KiB