欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2187 Beauty Contes[凸包]:求点集最远距离

程序员文章站 2022-04-02 17:01:20
...

题目大意

给定好多点,求出点与点之间的最短距离

思路分析

凸包裸体,详细教程

#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

#define MAX 50005
#define ll long long

struct P {
	ll x, y;
	P(ll a = 0, ll b = 0) { x = a, y = b; }
	ll mul(P p) {
		return (x*p.y - p.x*y);
	}
	P operator - (const P &p) { return P(x - p.x, y - p.y); }
}a[MAX];

vector<P> v(MAX);

bool cmp(P p1, P p2) {
	if (p1.x == p2.x)return p1.y < p2.y;
	else return p1.x < p2.x;
}

ll dis(P p1, P p2) {
	return (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y);
}

int main() {
	int N, k = 0; ll res = 0; scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		scanf("%lld%lld", &a[i].x, &a[i].y);
	}

	sort(a, a + N, cmp);

	for (int i = 0; i < N; i++) {
		while (k > 1 && (a[i] - v[k - 1]).mul(v[k - 1] - v[k - 2]) > 0)k--;
		v[k++] = a[i];
	}
	//N-1必然在凸集内部  t=k:保证下边界的点集不被删除
	for (int i = N - 2, t = k; i >= 0; i--) {
		while (k > t && (a[i] - v[k - 1]).mul(v[k - 1] - v[k - 2]) > 0)k--;
		v[k++] = a[i];
	}

	for (int i = 0; i < k; i++) {
		for (int j = i + 1; j < k; j++) {
			ll d = dis(v[i], v[j]);
			if (d > res)res = d;
		}
	}

	printf("%lld\n", res);
}
相关标签: ACM空间计算几何