Beauty Contest
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 35327 | Accepted: 10935 |
Description
Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.
Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms.
Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms.
Input
* Line 1: A single integer, N
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.
Sample Input
4 0 0 0 1 1 1 1 0
Sample Output
2
Hint
Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)
题意:给你n个散点集,求两点之间的距离的最大值。
思路:这些散点以左下角的点为起点,构成一个凸包,那么距离的最大值的两点必定出现在凸包上。表示没用什么旋转卡壳,直接构造凸包暴力188ms
注意:
1.可能有重点。
2.当前点a是否在凸包上是与凸包集的最后一个点b和之前一个点c,ba叉乘cb向量的正负来决定的,这里学习了用栈存储。当前点不满足条件消除当前点,这样确保距离最大。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=50000+5;
const int inf=0x3f3f3f3f;
struct node
{
int x,y;
}p[MAXN];
int tu[MAXN];
//求距离平方
int dis(node a,node b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
//求ab×cd
int det(int x1,int y1,int x2,int y2)
{
return x1*y2-x2*y1;
}
int cross(node a,node b,node c,node d)
{
return det(b.x-a.x, b.y-a.y, d.x-c.x, d.y-c.y);
}
node p0;//左下角的起始点
bool cmp(node a,node b)//比较函数,按照极坐标从下到大排序,极坐标相同按照距原点的距离从小到大
{
int temp=cross(p0,a,p0,b);
if(temp)
{
return temp>0;
}
else return dis(p0,a)<dis(p0,b);
}
int main()
{
int i,j;
int n;
int mx=inf,pos;
scanf("%d",&n);
for(i=0;i<n;++i)
{
scanf("%d%d",&p[i].x,&p[i].y);
if(mx>p[i].x)
{
mx=p[i].x;
pos=i;
}
else if(mx==p[i].x)
{
if(p[pos].y>p[i].y)pos=i;
}
}
p0=p[pos];
sort(p,p+n,cmp);
int top=0;
tu[0]=0;
tu[++top]=1;
tu[n]=0;
for(i=2;i<=n;)//构造凸包
{
if(cross(p[tu[top-1]],p[tu[top]],p[tu[top]],p[i])>=0)
{
tu[++top]=i++;
}
else
{
--top;
}
}
int maxdis=0;
for(i=0;i<top;++i)
for(j=i+1;j<=top;++j)
{
maxdis=max(maxdis,dis(p[tu[i]],p[tu[j]]));
}
printf("%d\n",maxdis);
return 0;
}