31602023-02-21 11:21:25TuruTamasHírvivők csoportosítása (40)cpp17Wrong answer 6/4017ms4700 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 = (N+M)/2;
    int lastn = n;
    while (true) {
        int csoportok;
        getGroups(csoportok, n);
        if (csoportok > K) {
            minn = n;
            n = (n+maxn)/2;
            if (n == lastn) {
                for (size_t i = n; i < N+M; i++)
                {
                    getGroups(csoportok, i);
                    if (csoportok < K) {
                        cout << i;
                        return 0;
                    }
                }
            }
            
            lastn = n;
        }
        else if (csoportok < K) {
            maxn = n;
            n = (n+minn)/2;
            if (n == lastn) {
                cout << n;
                return 0;
            }
            lastn = n;
        }
        else {
            cout << n << "\n";
            return 0;
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base6/40
1Wrong answer0/03ms1752 KiB
2Accepted0/017ms2076 KiB
3Accepted1/13ms2124 KiB
4Accepted1/13ms2340 KiB
5Wrong answer0/13ms2588 KiB
6Wrong answer0/14ms2804 KiB
7Wrong answer0/13ms2976 KiB
8Wrong answer0/13ms3188 KiB
9Wrong answer0/13ms3400 KiB
10Accepted1/16ms3648 KiB
11Wrong answer0/14ms3860 KiB
12Wrong answer0/14ms3944 KiB
13Wrong answer0/314ms3972 KiB
14Wrong answer0/313ms3968 KiB
15Wrong answer0/34ms3948 KiB
16Wrong answer0/313ms4200 KiB
17Wrong answer0/34ms4160 KiB
18Wrong answer0/310ms4232 KiB
19Wrong answer0/310ms4300 KiB
20Wrong answer0/314ms4232 KiB
21Accepted3/310ms4484 KiB
22Wrong answer0/34ms4700 KiB