博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2187:Beauty Contest(旋转卡壳)
阅读量:6306 次
发布时间:2019-06-22

本文共 2977 字,大约阅读时间需要 9 分钟。

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 32708   Accepted: 10156

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

40 00 11 11 0

Sample Output

2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 
 
题解:
  平面中给定N个点,让你求距离最远的点对。首先距离最远的一对点肯定在凸包上,所以可以先用Graham算法搞一遍凸包。
  如果枚举凸包的顶点,则效率太低。根据旋转卡壳,我们设两条平行线,当一条平行线与一条凸包上的边重合时,另一条平行线所过的点与离其最远的点一定是与凸包重合的那条平行线上的两点之一。由此性质,我们可以逆时针枚举凸包上的边,然后找离这条边距离最远的点,用这个点与线段两端点的距离更新答案。判断距离相等的条件是三点构成三角形的面积最大,这个可以用向量的叉积判断。
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 using namespace std;10 const double eps=1e-8;11 int top,N;12 int ANS;13 struct P{14 int x,y;15 friend P operator-(P a,P b){16 P t; t.x=a.x-b.x; t.y=a.y-b.y;17 return t;18 }19 friend double operator*(P a,P b){20 return a.x*b.y-b.x*a.y;21 }22 }p[50005],s[50005];23 24 inline int dis(P a,P b){25 return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);26 }27 inline bool operator<(P a,P b){28 int t=(a-p[1])*(b-p[1]);29 if(abs(t)<=eps) return dis(a,p[1])
0;31 }32 inline void graham(){33 int tmp=1;34 for(int i=2;i<=N;i++){35 if(p[i].y
1&&(p[i]-s[top-1])*(s[top]-s[top-1])>=0) top--;42 s[++top]=p[i];43 }44 }45 inline int RC(){46 int q=2; ANS=0;47 s[top+1]=p[1];48 for(int i=1;i<=top;i++){49 while((s[i+1]-s[i])*(s[q+1]-s[i])>(s[i+1]-s[i])*(s[q]-s[i])) q=q%top+1;50 ANS=max(ANS,max(dis(s[q],s[i+1]),dis(s[q],s[i])));51 }52 return ANS;53 }54 int main(){55 scanf("%d",&N);56 for(int i=1;i<=N;i++){57 scanf("%d%d",&p[i].x,&p[i].y);58 }59 graham();60 printf("%d",RC());61 return 0;62 }

 

转载于:https://www.cnblogs.com/CXCXCXC/p/5249416.html

你可能感兴趣的文章
jqueryValidate
查看>>
ATL使用IE控件,并且屏蔽右键
查看>>
Jenkins
查看>>
linux下使用screen和ping命令对网络质量进行监控
查看>>
数据库设计技巧
查看>>
css定位概述
查看>>
C# 动态修改配置文件 (二)
查看>>
BOM:文档对象模型 --树模型
查看>>
我的Android进阶之旅------>WindowManager.LayoutParams介绍
查看>>
segment
查看>>
获取鼠标的原始移动值
查看>>
Linux信号 编程
查看>>
有关滚动与位置
查看>>
Box2D自定义重力
查看>>
chpasswd
查看>>
mysqldump --single-transaction 和--lock-tables参数详解
查看>>
android 数据库_sql语句总结
查看>>
python购物车
查看>>
解决python2和python3的pip冲突
查看>>
面试/编程
查看>>