POJ 2187 Beauty Contest(凸包)

题意:题目的意思就是说给你N个点,然后让你求出最远的两个点的距离的平方(注意:是平方);

分析:对于50000个点暴力肯定是不行的,那么我们可以联系到凸包,因为最远的点肯定是在凸包上,那么要求最远的
距离,我们就可以先对这些点求一次凸包(复习凸包的写法)。然后对于凸包上面的点可以枚举,因为这时候要枚举的
点的数目已经减少很多了,这时候,就是普通的暴搞了。主要是前面的凸包的写法。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdio>
#define N 50005
using namespace std;

struct node{
	int x, y;
	bool friend operator < (const node &a, const node &b){
		if(a.y == b.y)
			return a.x < b.x;
		return a.y < b.y;
	}
}point[N];

double calcurateDis(node a, node b){
	return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
}

bool judge(node a, node b, node c){
	return (a.x - c.x)*(b.y - c.y) >= (b.x - c.x)*(a.y - c.y);
}

double solve(int n){
	vector<node> tempPoint(n*2);
	sort(point, point + n);
	if(n == 1){
		return 0;
	}
	if(n == 2){
		return calcurateDis(point[0], point[1]);
	}
	tempPoint[0] = point[0];
	tempPoint[1] = point[1];
	tempPoint[2] = point[2];
	int top = 1;
	for(int i = 2; i < n; i++){
		while(top != 0 && judge(point[i], tempPoint[top], tempPoint[top - 1]))	top--;
		tempPoint[++top] = point[i];
	}
	int len = top;
	tempPoint[++top] = point[n - 2];
	for(int i = n - 3; i >= 0; i--){
		while(top != len && judge(point[i], tempPoint[top], tempPoint[top - 1]))	top--;
		tempPoint[++top] = point[i];
	}
	double result = 0.0;
	for(int i = 0; i < top; ++i)
		for(int j = i + 1; j < top; ++j)
			result = max(result, calcurateDis(tempPoint[i], tempPoint[j]));
	return result;
}

int main(){
	int n;
	while(~scanf("%d", &n)){
		for(int i = 0; i < n; i++)
			scanf("%d %d", &point[i].x, &point[i].y);

		printf("%.0lf\n", solve(n));
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值