您的位置:首页 > 其它

Beauty Contest(POJ_2187)

2016-01-27 15:48 344 查看

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)

Source

USACO 2003 Fall

代码

//这是第一道自己真正理解了做的凸包,不是完全照抄模板呦~233~虽然是学的白书上的写法~

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct Point
{
double x, y;
bool operator < (const Point b)const
{
if(x != b.x)
return x < b.x;
return y < b.y;
}
Point operator - (const Point b)
{
Point c;
c.x = x - b.x;
c.y = y - b.y;
return c;
}
} point[50100];

Point ans[50100];
int cnt;

double Cross(Point a, Point b) //叉积 -> 性质: v在w左边, 则Cross(v,w) < 0, 反之 > 0。
{
return a.x * b.y - a.y * b.x;
}

void ConvexHull(int n)
{
cnt = 0;
for(int i = 0; i < n; i++)
{
while(cnt > 1 && Cross(ans[cnt-1] - ans[cnt-2], point[i] - ans[cnt-2]) < 0)
cnt--;
ans[cnt++] = point[i];
}
int k = cnt;
for(int i = n - 2; i >= 0; i--)
{
while(cnt > k && Cross(ans[cnt-1] - ans[cnt-2], point[i] - ans[cnt-2]) < 0)
cnt--;
ans[cnt++] = point[i];
}
if(n > 1) cnt--; //此处没搞懂
}

int main()
{
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%lf %lf", &point[i].x, &point[i].y);
}
sort(point, point+n);
ConvexHull(n);
double res = -1;
for(int i = 0; i < cnt; i++)
{
for(int j = i + 1; j < cnt; j++)
{
double a = (ans[i].x-ans[j].x) * (ans[i].x-ans[j].x) + (ans[i].y-ans[j].y) * (ans[i].y-ans[j].y);
if(a > res)
res = a;
}
}
printf("%.0f\n", res);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: