106642024-04-07 19:26:54Valaki2Rajzcpp17Accepted 100/100384ms22892 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second

struct point {
    int x, y;
    int id;
    point() : x(0), y(0), id(-1) {}
    point(int x_, int y_, int id_) :
        x(x_), y(y_), id(id_) {}
};

int sg(int x) {
    if(x < 0) {
        return -1;
    }
    if(x > 0) {
        return 1;
    }
    return 0;
}

int direction(point a, point b, point c) {
    return sg((c.y - a.y) * (b.x - a.x) - (b.y - a.y) * (c.x - a.x));
}

bool between(point a, point b, point c) {
    return c.x >= min(a.x, b.x) && c.x <= max(a.x, b.x) && c.y >= min(a.y, b.y) && c.y <= max(a.y, b.y);
}

bool on_segment(point a, point b, point c) {
    return direction(a, b, c) == 0 && between(a, b, c);
}

bool intersect(point a, point b, point c, point d) {
    return on_segment(a, b, c) || on_segment(a, b, d) || on_segment(c, d, a) || on_segment(c, d, b)
        || (direction(a, b, c) * direction(a, b, d) == -1 && direction(c, d, a) * direction(c, d, b) == -1);
}

const int maxn = 1000;
const int inf = 1e9 + 7;
const point far = point(1, -inf, -1);
const point origin = point(0, 0, -1);

int n;
point points[1 + maxn];
unsigned int dist[1 + maxn][1 + maxn];
bool intersects[1 + maxn][1 + maxn];
int vis[1 + maxn];
unsigned int len;
int cur_comp;

void dfs(int cur) {
    vis[cur] = cur_comp;
    for(int nei = 1; nei <= n; nei++) {
        if(nei == cur || vis[nei] || dist[cur][nei] > len || intersects[cur][nei]) {
            continue;
        }
        dfs(nei);
    }
}

bool good(unsigned int cur_len) {
    len = cur_len;
    cur_comp = 0;
    for(int i = 1; i <= n; i++) {
        vis[i] = false;
    }
    for(int i = 1; i <= n; i++) {
        if(!vis[i]) {
            cur_comp++;
            dfs(i);
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(i != j && vis[i] == vis[j] && dist[i][j] <= len && intersects[i][j]) {
                return true;
            }
        }
    }
    return false;
}

void solve() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> points[i].x >> points[i].y;
        points[i].id = i;
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            dist[i][j] = (points[i].x - points[j].x) * (points[i].x - points[j].x)
                + (points[i].y - points[j].y) * (points[i].y - points[j].y);
            intersects[i][j] = intersect(points[i], points[j], origin, far);
        }
    }
    unsigned int l = 0, r = 8e18 + 1;
    while(l < r - 1) {
        unsigned int mid = (l + r) / 2;
        if(good(mid)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    if(r == 8e18 + 1) {
        cout << "-1\n";
    } else {
        cout << r << "\n";
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1952 KiB
2Accepted3ms2148 KiB
subtask220/20
3Accepted3ms2420 KiB
4Accepted3ms2632 KiB
5Accepted3ms2532 KiB
6Accepted3ms2788 KiB
7Accepted3ms3000 KiB
8Accepted3ms2952 KiB
9Accepted3ms2952 KiB
10Accepted3ms2956 KiB
11Accepted3ms2920 KiB
12Accepted3ms3372 KiB
subtask320/20
13Accepted3ms2420 KiB
14Accepted3ms2632 KiB
15Accepted3ms2532 KiB
16Accepted3ms2788 KiB
17Accepted3ms3000 KiB
18Accepted3ms2952 KiB
19Accepted3ms2952 KiB
20Accepted3ms2956 KiB
21Accepted3ms2920 KiB
22Accepted3ms3372 KiB
23Accepted3ms3492 KiB
24Accepted3ms3704 KiB
25Accepted3ms3912 KiB
26Accepted3ms3892 KiB
27Accepted3ms4128 KiB
28Accepted3ms4152 KiB
29Accepted3ms4236 KiB
30Accepted3ms4364 KiB
31Accepted3ms4332 KiB
subtask420/20
32Accepted3ms2420 KiB
33Accepted3ms2632 KiB
34Accepted3ms2532 KiB
35Accepted3ms2788 KiB
36Accepted3ms3000 KiB
37Accepted3ms2952 KiB
38Accepted3ms2952 KiB
39Accepted3ms2956 KiB
40Accepted3ms2920 KiB
41Accepted3ms3372 KiB
42Accepted3ms3492 KiB
43Accepted3ms3704 KiB
44Accepted3ms3912 KiB
45Accepted3ms3892 KiB
46Accepted3ms4128 KiB
47Accepted3ms4152 KiB
48Accepted3ms4236 KiB
49Accepted3ms4364 KiB
50Accepted3ms4332 KiB
51Accepted4ms5404 KiB
52Accepted4ms5388 KiB
53Accepted4ms5400 KiB
54Accepted4ms5520 KiB
55Accepted4ms5764 KiB
56Accepted7ms5768 KiB
57Accepted7ms5768 KiB
58Accepted7ms5768 KiB
59Accepted6ms5780 KiB
60Accepted6ms5776 KiB
61Accepted6ms5908 KiB
62Accepted6ms5688 KiB
63Accepted6ms5916 KiB
64Accepted6ms6020 KiB
65Accepted6ms5904 KiB
66Accepted4ms5884 KiB
67Accepted6ms6008 KiB
68Accepted6ms6140 KiB
69Accepted4ms6196 KiB
70Accepted6ms6188 KiB
subtask540/40
71Accepted3ms2420 KiB
72Accepted3ms2632 KiB
73Accepted3ms2532 KiB
74Accepted3ms2788 KiB
75Accepted3ms3000 KiB
76Accepted3ms2952 KiB
77Accepted3ms2952 KiB
78Accepted3ms2956 KiB
79Accepted3ms2920 KiB
80Accepted3ms3372 KiB
81Accepted3ms3492 KiB
82Accepted3ms3704 KiB
83Accepted3ms3912 KiB
84Accepted3ms3892 KiB
85Accepted3ms4128 KiB
86Accepted3ms4152 KiB
87Accepted3ms4236 KiB
88Accepted3ms4364 KiB
89Accepted3ms4332 KiB
90Accepted4ms5404 KiB
91Accepted4ms5388 KiB
92Accepted4ms5400 KiB
93Accepted4ms5520 KiB
94Accepted4ms5764 KiB
95Accepted7ms5768 KiB
96Accepted7ms5768 KiB
97Accepted7ms5768 KiB
98Accepted6ms5780 KiB
99Accepted6ms5776 KiB
100Accepted6ms5908 KiB
101Accepted6ms5688 KiB
102Accepted6ms5916 KiB
103Accepted6ms6020 KiB
104Accepted6ms5904 KiB
105Accepted4ms5884 KiB
106Accepted6ms6008 KiB
107Accepted6ms6140 KiB
108Accepted4ms6196 KiB
109Accepted6ms6188 KiB
110Accepted172ms22748 KiB
111Accepted177ms22784 KiB
112Accepted184ms22780 KiB
113Accepted189ms22868 KiB
114Accepted180ms22732 KiB
115Accepted180ms22712 KiB
116Accepted291ms22892 KiB
117Accepted333ms22796 KiB
118Accepted384ms22804 KiB
119Accepted310ms22720 KiB
120Accepted337ms22684 KiB
121Accepted317ms22720 KiB
122Accepted259ms22744 KiB
123Accepted266ms22684 KiB
124Accepted277ms22716 KiB
125Accepted270ms22708 KiB
126Accepted277ms22684 KiB
127Accepted246ms22716 KiB
128Accepted289ms22804 KiB
129Accepted222ms22748 KiB
130Accepted233ms22748 KiB
131Accepted195ms22744 KiB
132Accepted194ms22780 KiB