poj 2187 Beauty Contest(二维凸包旋转卡壳)
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
2
Hint
#include<cstdio>
#include<cmath>
#include<algorithm>
#define MAX 50010
using namespace std;
struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y){}
};
Point P[MAX],ch[MAX];
typedef Point Vector;
Vector operator - (Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
bool operator <(const Point &a,const Point &b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
double Length(Vector A)
{
return A.x*A.x+A.y*A.y;
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
int ConvexHull(Point *p,int n)
{
sort(p,p+n);
int m=;
for(int i=;i<n;i++)
{
while(m>&&Cross(ch[m-]-ch[m-],p[i]-ch[m-])<=) m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-;i>=;i--)
{
while(m>k&&Cross(ch[m-]-ch[m-],p[i]-ch[m-])<=) m--;
ch[m++]=p[i];
}
if(n>) m--;
return m;
}
double rotating_calipers(int n)
{
int q=;
double ans=0.0;
ch[n]=ch[];
for(int p=;p<n;p++) //p是边 q是点
{
while(Cross(ch[p+]-ch[p],ch[q+]-ch[p])>Cross(ch[p+]-ch[p],ch[q]-ch[p]))
q=(q+)%n;
ans=max(ans,max(Length(ch[p]-ch[q]),Length(ch[p+]-ch[q])));
}
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
scanf("%lf%lf",&P[i].x,&P[i].y);
int m=ConvexHull(P,n);
double ans=rotating_calipers(m);
printf("%0.0lf\n",ans);
}
return ;
}
//注意不能像C题uva10652一样用pc,因为那个题里是pc++,这里pc是0。
最新文章
- 两种常用的C语言排序算法
- [LeetCode] Symmetric Tree 判断对称树
- 1、B2BUA
- 【Java基础】序列化与反序列化深入分析
- fastcgi安装
- create thread的时候发生core dump
- UIStepper步进器 ——事件驱动型控件,(一个+和-按钮的)
- Python基础学习笔记(十)日期Calendar和时间Timer
- preventDefault stopPropagation??
- asp.net mvc4 easyui datagrid 增删改查分页 导出 先上传后导入 NPOI批量导入 导出EXCEL
- Codeforces 474D Flowers
- Android Gradle配置
- Android---App Widget(一)
- C#面试题记录
- Git:常用功能 - 命令行
- 【ZJOI2010】网络扩容
- Tomcat8.0 配置环境
- R-- Dplyr包
- String是值传递还是引用传递
- 【转】像素 Pixel (Picture Element)