计算几何入门4--poj2187Beauty Contest
2016-01-09 17:04
267 查看
题目大意:求平面上的点的最大距离的平方
造出一个凸包,利用面积的上凸性(一条线段和凸包上其他点组成的三角形的面积),用两条直线卡住凸包求最大距离
造出一个凸包,利用面积的上凸性(一条线段和凸包上其他点组成的三角形的面积),用两条直线卡住凸包求最大距离
#include<map> #include<set> #include<cmath> #include<queue> #include<cstdio> #include<vector> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=50000+10; int n,m,top=0; struct point{ int x,y; }p[maxn]; point sta[maxn*2]; int cal(point a,point b){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } bool cmp(const point a,const point b){ return (a.x<b.x||(a.x==b.x&&a.y<b.y)); } int crossx(point a,point b,point c){ return (c.y-a.y)*(b.x-a.x)-(b.y-a.y)*(c.x-a.x); } void graham(){ memset(sta,0,sizeof(sta)); top=0; sta[++top]=p[1]; sta[++top]=p[2]; for (int i=3;i<=n;++i){ while (top>1&&crossx(sta[top-1],sta[top],p[i])<=0) top--; sta[++top]=p[i]; } int tmp=top; sta[++top]=p[n-1]; for (int i=n-2;i>=1;--i){ while (top>tmp&&crossx(sta[top-1],sta[top],p[i])<=0) top--; sta[++top]=p[i]; } } void xzqk(){ int pos=2; int ans=0; for (int i=1;i<=top;++i){ while (crossx(sta[i],sta[i+1],sta[pos])<crossx(sta[i],sta[i+1],sta[pos+1])){ pos++; if (pos==top) pos=1; } ans=max(ans,max(cal(sta[i],sta[pos]),cal(sta[i+1],sta[pos+1]))); } printf("%d",ans); } int main(){ while (scanf("%d",&n)!=EOF){ for (int i=1;i<=n;++i) scanf("%d%d",&p[i].x,&p[i].y); if (n==2){ printf("%d\n",cal(p[1],p[2])); continue; }else if (n<2){ printf("%d\n",0); continue; } sort(p+1,p+n+1,cmp); graham(); xzqk(); } }
相关文章推荐
- C++类中的常量
- JMS - ConnectionMetaData
- jdbc 学习总结
- 用SSH做采购系统遇到的问题
- hdu 2562 奇偶位互换
- 6to4 自动隧道技术
- V210 UART TX 流程
- oracle表空间
- ORA-27603: Cell storage I/O error
- 关于NJKWebViewProgress的纯代码编写。
- table中设置tr行间距
- MySQL单表百万数据记录分页性能优化 limit优化
- 虚幻4随笔4 从project開始
- iOS 基础 第一天(0804)
- 史上最全面的正则表达式
- 级联映射关系的分页查询ManyToMany的notIn子查询subquery实现。notMember 、notExist
- 反射(二)Class类,Method类,Field类,反射获得带参数的对象
- C#集合
- resid入门笔记(二)
- 兄弟