Beauty Contest
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 34373 | Accepted: 10640 |
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)
求的是平面点集上的最远点对,实际上就是该点集的凸包的直径, 构建出凸包后枚举两点求最远距离
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
struct node
{
int x;
int y;
} point[100000];
int n;
int s[100000], top;
double chacheng(struct node a, struct node b, struct node c)
{
return ((a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x));
}
double dist(struct node a, struct node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(struct node a,struct node b)
{
int k=chacheng(a,b,point[0]);
if(k>0)
return 1;
else if(k==0&&dist(a,point[0])<dist(b,point[0]))
return 1;
else
return 0;
}
void Graham()
{
int k=0;
int i;
for(i=1; i<n; i++)
{
if (point[i].x<point[k].x||(point[i].x==point[k].x&&point[i].y<point[k].y))
{
k=i;
}
}
swap(point[0],point[k]);
sort(point,point+n,cmp);
for(i=0; i<=2; i++)
{
s[i]=i;
}
top=2;
for(i=3; i<n; i++)
{
while(chacheng(point[i],point[s[top]],point[s[top-1]])>=0)
{
top--;
if(top==0)
{
break;
}
}
top++;
s[top]=i;
}
}
int dis(node a, node b)
{
int x = a.x-b.x, y = a.y-b.y;
return fabs(x*x+y*y);
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
scanf("%d%d",&point[i].x,&point[i].y);
Graham();
int maxdist=0;
for(int i=0;i<=top-1;i++)
for(int j=i+1;j<=top;j++)
{
int temp=dis(point[ s[i] ],point[ s[j] ]);
if(maxdist < temp)
maxdist = temp;
}
printf("%d\n", maxdist);
}
return 0;
}