55602023-07-27 11:18:24zsomborDrónfutárcpp17Accepted 100/100266ms20892 KiB
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;

struct point{
    int x,y,ind;
};

int n;
vector <point> v;
vector <pair <int,pair <int,int>>> e;
map <int,int> m;

bool srt(point a,point b){
    return (a.x+a.y<b.x+b.y);
}

int dist(int i,int j){
    int a=abs((v[i].x+v[i].y)-(v[j].x+v[j].y));
    int b=abs((v[i].x-v[i].y)-(v[j].x-v[j].y));
    return max(a,b);
}

void get_edges(){
    sort(v.begin(),v.end(),srt);
    m.clear();
    for (int i=0;i<n;i++){
        auto it=m.lower_bound(v[i].x-v[i].y);
        while(it!=m.end()){
            int j=(*it).second;
            if (v[j].x>v[i].x) break;
            it=m.erase(it);
            pair <int,pair<int,int>>p;
            p.first=dist(i,j);
            p.second.first=min(v[i].ind,v[j].ind);
            p.second.second=max(v[i].ind,v[j].ind);
            e.push_back(p);
        }
        m[v[i].x-v[i].y]=i;
    }
}

void geometry(){
    get_edges();
    for (int i=0;i<n;i++) v[i].y*=-1;
    get_edges();
    for (int i=0;i<n;i++) swap(v[i].x,v[i].y);
    get_edges();
    for (int i=0;i<n;i++) v[i].y*=-1;
    get_edges();
}

//------------------------------

int cnt;
vector <int> par(1e5,0);
vector <int> sz(1e5,1);

int holvan(int a){
    return (a==par[a]?a:par[a]=holvan(par[a]));
}

void unio(int a,int b){
    a=holvan(a);
    b=holvan(b);
    if (a==b) return;
    if (sz[a]>sz[b]) swap(a,b);
    par[a]=b;
    sz[b]+=sz[a];
    cnt--;
}

int spanning_tree(){
    cnt=n;
    for (int i=0;i<n;i++) par[i]=i;
    sort(e.begin(),e.end());
    int a,b,w;
    for (auto p : e){
        a=p.second.first;
        b=p.second.second;
        w=p.first;
        unio(a,b);
        if (cnt==1) return w;
    }
    return -1;
}

//------------------------------

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    v.resize(n);
    for (int i=0;i<n;i++){
        cin >> v[i].x >> v[i].y;
        v[i].ind=i;
    }
    geometry();
    cout << spanning_tree();
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms3160 KiB
2Accepted263ms18492 KiB
subtask215/15
3Accepted4ms4136 KiB
4Accepted4ms4224 KiB
5Accepted4ms4448 KiB
6Accepted4ms4524 KiB
7Accepted3ms4352 KiB
subtask315/15
8Accepted4ms4136 KiB
9Accepted4ms4224 KiB
10Accepted4ms4448 KiB
11Accepted4ms4524 KiB
12Accepted3ms4352 KiB
13Accepted4ms4436 KiB
14Accepted4ms4816 KiB
15Accepted4ms5052 KiB
16Accepted4ms5136 KiB
17Accepted4ms5184 KiB
subtask435/35
18Accepted4ms4136 KiB
19Accepted4ms4224 KiB
20Accepted4ms4448 KiB
21Accepted4ms4524 KiB
22Accepted3ms4352 KiB
23Accepted4ms4436 KiB
24Accepted4ms4816 KiB
25Accepted4ms5052 KiB
26Accepted4ms5136 KiB
27Accepted4ms5184 KiB
28Accepted4ms5160 KiB
29Accepted50ms8612 KiB
30Accepted75ms9016 KiB
31Accepted75ms9356 KiB
32Accepted63ms11120 KiB
subtask535/35
33Accepted4ms4136 KiB
34Accepted4ms4224 KiB
35Accepted4ms4448 KiB
36Accepted4ms4524 KiB
37Accepted3ms4352 KiB
38Accepted4ms4436 KiB
39Accepted4ms4816 KiB
40Accepted4ms5052 KiB
41Accepted4ms5136 KiB
42Accepted4ms5184 KiB
43Accepted4ms5160 KiB
44Accepted50ms8612 KiB
45Accepted75ms9016 KiB
46Accepted75ms9356 KiB
47Accepted63ms11120 KiB
48Accepted128ms12780 KiB
49Accepted266ms20344 KiB
50Accepted266ms20668 KiB
51Accepted266ms20868 KiB
52Accepted266ms20892 KiB