56802023-09-07 22:15:18TuruTamasMobilNet (50 pont)cpp17Időlimit túllépés 32/501.078s16268 KiB
//
// Created by tamas on 9/7/23.
//

// sztem kicsit ronda, de hát na

#include "bits/stdc++.h"
using namespace std;

#define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
#define ull unsigned long long

int N;
ull top, bot, T;

typedef pair<ull, ull> coord;

vector<coord> by_x;
vector<coord> by_y;
map<coord, bool> visited;

auto sort_by_x = [](coord a, coord b) {
    if (a.first == b.first)
        return a.second > b.second;
    return a.first < b.first;
};

auto sort_by_y = [](coord a, coord b) {
    if (a.second == b.second)
        return a.first > b.first;
    return a.second > b.second;
};

void dfs(coord x) {
//    cout << "in " << x.first << " " << x.second << "\n";
    visited[x] = true;
    long ind = std::lower_bound(by_x.begin(), by_x.end(), x, sort_by_x) - by_x.begin();
    long i = ind-1;
    while (i >= 0 && by_x[i].first == x.first && by_x[i].second - x.second <= T) {
        if (!visited[by_x[i]]) {
            dfs(by_x[i]);
        }
        i--;
    }
    i = ind+1;
    while (i < N && by_x[i].first == x.first && x.second - by_x[i].second <= T) {
        if (!visited[by_x[i]]) {
            dfs(by_x[i]);
        }
        i++;
    }

    ind = std::lower_bound(by_y.begin(), by_y.end(), x, sort_by_y) - by_y.begin();
    i = ind-1;
    while (i >= 0 && by_y[i].second == x.second && by_y[i].first - x.first <= T) {
        if (!visited[by_y[i]]) {
            dfs(by_y[i]);
        }
        i++;
    }
    i = ind+1;
    while (i < N && by_y[i].second == x.second && x.first - by_y[i].first <= T ) {
        if (!visited[by_y[i]]) {
            dfs(by_y[i]);
        }
        i--;
    }
}

bool good() {
    for (auto& kv : visited) {
        kv.second = false;
    }
//    cout << "T=" << T << ":\n";
    dfs(by_x[0]);
    bool r = true;
    for (auto p : visited)
        if (!p.second)
            r = false;
    return r;
}

int gut() {
    for (auto& kv : visited) {
        kv.second = false;
    }
//    cout << "T=" << T << ":\n";
    int r = -1;
    T--;
    for (auto p : visited)
        if (!p.second) {
            r++;
            dfs(p.first);
        }
    T++;
    return r;
}

int main() {
    InTheNameOfGod
    cin >> N;
    for (int i = 0; i < N; i++) {
        ull x, y;
        cin >> x >> y;
        x--; y--;
        visited.emplace(make_pair(x, y), false);
        by_x.emplace_back(x, y);
        by_y.emplace_back(x, y);
    }
    std::sort(by_y.begin(), by_y.end(), sort_by_y);
    std::sort(by_x.begin(), by_x.end(), sort_by_x);
    bot = 1;
    top = 10'000'001;
    T = (top/N)/2;
    while (bot <= top) {
        if (good())
            top = T - 1;
        else
            bot = T + 1;
        T = (bot + top) / 2;
    }
    T++; // idk miért
    cout << T << "\n" << gut();
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base32/50
1Elfogadva0/03ms2168 KiB
2Elfogadva0/0153ms3676 KiB
3Elfogadva2/24ms2368 KiB
4Elfogadva2/24ms2624 KiB
5Elfogadva2/23ms2692 KiB
6Elfogadva2/24ms3068 KiB
7Elfogadva2/27ms3320 KiB
8Elfogadva2/28ms3560 KiB
9Elfogadva2/228ms3960 KiB
10Elfogadva2/230ms3976 KiB
11Elfogadva2/279ms4876 KiB
12Elfogadva2/2137ms5172 KiB
13Elfogadva3/3310ms6864 KiB
14Elfogadva3/3731ms8532 KiB
15Elfogadva3/3910ms10108 KiB
16Időlimit túllépés0/31.078s7532 KiB
17Időlimit túllépés0/31.057s8516 KiB
18Elfogadva3/3711ms16268 KiB
19Időlimit túllépés0/31.07s10820 KiB
20Időlimit túllépés0/31.057s13316 KiB
21Időlimit túllépés0/31.077s11732 KiB
22Időlimit túllépés0/31.052s11204 KiB