您的位置:首页 > 其它

POJ-2187--Beauty Contest---凸包

2017-07-28 10:58 351 查看
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.

Input

* Line 1: A single integer, N

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个点,然后找距离最远的点的距离然后平方。

简单凸包模板题,直接上代码。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#define sf scanf
#define pf printf
using namespace std;
const int M=50000+10;
const int MAX=0x3f3f3f3f;
double xx=MAX,yy=MAX;
struct point
{
double x;
double y;
} s[M+50],p[M+50];
double cross(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
double compare(point a,point b,point c)
{
return cross(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}
bool cmp(point a,point b)
{
point c;
c.x=0;
c.y=0;
if(compare(a,b,c)==0)
return a.x<b.x;
else
return compare(a,b,c)>0;
}
double Caculate(double x1,double y1,double x2,double y2)
{
return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
}//这里的函数用来计算距离
int main()
{
int n;
sf("%d",&n);
int i;
int t=0;
for(i=0; i<=n-1; i++)
{
sf("%lf%lf",&p[i].x,&p[i].y);
if(p[i].y<yy)
{
xx=p[i].x;
yy=p[i].y;
t=i;
}
}
p[t]=p[0];
sort(p+1,p+n,cmp);
int top=1;
s[0].x=xx;
s[0].y=yy;
s[1]=p[1];
for(i=2; i<=n-1;)
{
if(top!=0&&compare(s[top-1],s[top],p[i])<0)
top--;
else
s[++top]=p[i++];
}
int Max=0;
for(i=0; i<=top; i++)
{
for(int j=i+1; j<=top; j++)
{
if(Caculate(s[i].x,s[i].y,s[j].x,s[j].y)>Max)
Max=Caculate(s[i].x,s[i].y,s[j].x,s[j].y);
}
}//因为凸包之后的点数量会很少,所以直接遍历寻找最大距离的点
cout<<Max<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: