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 goodwillbetween 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;
}
相关文章推荐
- js中table的值进行点击排序
- Android之服务(四)前台服务
- SpringMVC基础-SpringMVC的四种请求方式
- 封装cookies
- C语言中time函数
- Spring+struts联网正常,断网启动报错:java.net.UnknownHostException: struts.apache.org
- JS 学习1--开始
- Android 检查运行慢原因和分析
- DP备份任务失败原因解析
- Maven管理Oracle JDBC驱动
- 行转列逗号隔开&逗号隔开列转行
- caffe里用到的五种调节学习率的方法
- 场景识别“Learning Deep Features for Scene Recognition using Places Database”
- 冬令营总结&寒假计划
- 使用handler内存泄漏解决
- 计算时间复杂度
- ACM-计算几何之Quoit Design——hdu1007 zoj2107
- Hadoop温故(一)基础篇
- 史上最详细的Android Studio系列教程二--基本设置与运行
- 数位dp入门 hdu2089 不要62