poj 2187 Beauty Contest (计算几何)
来源:互联网 发布:软件开发部门ios 编辑:程序博客网 时间:2024/05/17 00:19
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 34823 Accepted: 10772
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
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)
Source
USACO 2003 Fall
[Submit] [Go Back] [Status] [Discuss]
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define eps 1e-8#define N 50003using namespace std;int n,m,tot;struct vector{ double x,y;vector(double X=0,double Y=0){x=X,y=Y; };};vector operator -(vector a,vector b) {return vector(a.x-b.x,a.y-b.y);}vector operator +(vector a,vector b) {return vector(a.x+b.x,a.y+b.y);}vector operator *(vector a,double b) {return vector(a.x*b,a.y*b);}vector operator /(vector a,double b) {return vector(a.x/b,a.y/b);}bool operator ==(vector a,vector b){return b.x==a.x&&b.y==a.y;}bool operator <(const vector &a,const vector &b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}typedef vector point;point ch[N],p[N];int dcmp(double x){if (fabs(x)<eps) return 0;return x<0?-1:1;}double dot(vector a,vector b)//点积{return a.x*b.x+a.y*b.y;}double cross(vector a,vector b)//叉积,叉积为0,两直线平行{return a.x*b.y-a.y*b.x;}double len(vector a)//向量的模长{return sqrt(dot(a,a));}double distl(point p,point a,point b)//点到直线的距离,利用平行四边形的有向面积/底边向量的模长,得到的高即为点到直线的距离{vector v1,v2;v1=b-a; v2=p-a;return fabs(cross(v1,v2))/len(v1);}int convexhull(point* p,int n,point* ch)//Graham扫描法求凸包,把给定点包围在内,面积最小的凸多边形,注意该算法不能有重复的点,不能三点共线{sort(p,p+n);//把点按照X坐标排序int k,i,m=0;//注意是从0开始存储的 for (int i=0;i<n;i++)//把p1,p2放到凸包中,从p3开始,当当前点在凸包前进方向的左边时,加入该点;否则弹出最后加入的点,直到新点在左边,这样就确定了下凸壳{while(m>1&&dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) m--;ch[m++]=p[i];}k=m;for (int i=n-2;i>=0;i--)//然后反向确定上凸壳,因为之前下凸壳中的点是不能删去的所以m>k{while(m>k&&dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) m--;ch[m++]=p[i];}if (n>1) m--;return m;}double rotating_calipers(point* ch,int n)//旋转卡壳,被凸包上被一对平行直线卡住的点叫对踵点,最远点对一定在凸包的一对对踵点上{if (n==1) return 0;if (n==2) return dot(ch[0]-ch[1],ch[0]-ch[1]);//如果只有两个点,那么就是两点的直线距离int now=1,i;double ans=0;ch[n]=ch[0];for (int i=0;i<n;i++){while(dcmp(distl(ch[now],ch[i],ch[i+1])-distl(ch[now+1],ch[i],ch[i+1]))<=0)//最远点随着平行线的旋转是单调的,所以点不会来回移动 now=(now+1)%n;ans=max(ans,dot(ch[now]-ch[i],ch[now]-ch[i]));ans=max(ans,dot(ch[now]-ch[i+1],ch[now]-ch[i+1]));//找到与当前直线平行的最远点,用来更新答案}return ans;}int main(){scanf("%d",&n);for (int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);tot=convexhull(p,n,ch);printf("%.0lf\n",rotating_calipers(ch,tot));}
0 0
- poj 2187 Beauty Contest (计算几何)
- poj 2187 Beauty Contest 计算几何(凸包+旋转卡壳法)/最远点对
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- Poj 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187--Beauty Contest
- 归并排序
- 查看cloudrea的许可证---可用期限
- HDU 2126
- docker下tomcat环境搭建
- 一些东西+一些小错误。
- poj 2187 Beauty Contest (计算几何)
- 2016.08.14【初中部 NOIP提高组 】模拟赛C题解
- Wcf好教程
- makedown语言中中文输入 首行缩进-博客必备
- linux下MySQL安装及设置
- 如何解读Caffe源码
- 操作系统知识点
- poj 3608 Bridge Across Islands (计算几何)
- codeforces 189A. Cut Ribbon