poj 2187【凸包求最远距离】
来源:互联网 发布:红宝书软件下载 编辑:程序博客网 时间:2024/05/22 08:13
http://poj.org/problem?id=2187
这里很明显不能直接暴力(50000*50000)=。=
我们要知道最远的两点一定是凸包上的顶点,所以首先构造凸包,然后枚举找出最远距离,注意这里是平方!还要注意n==2的情况,直接求距离
#include<iostream>#include<vector>#include<map>#include<stack>#include<algorithm>#include<queue>#include<list>#include<set>#include<string.h>#include<stdlib.h>#include<math.h>#include<stdio.h>#include<ctype.h>#include<iomanip>using namespace std;#define LL long long#define pi acos(-1)#define N 50010#define INF 9999999999#define eps 1e-8int n;int top;struct point{ int x,y;}p[N],stck[N],p0;int cross(point a,point b,point c){ return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}int dis(point a,point b){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}bool cmp(point a,point b){ int t=cross(p0,a,b); return t>0 || (t==0 && dis(p0,a)<dis(p0,b));}void tubao(){ int i,j,k; k=0; for(i=1;i<n;i++) if(p[i].y<p[k].y || (p[i].y==p[k].y&&p[i].x<p[k].x)) k=i; p0=p[k]; p[k]=p[0]; p[0]=p0; sort(p+1,p+n,cmp); top=1; stck[0]=p[0]; stck[1]=p[1]; for(i=2;i<n;i++) { while(top>0&&cross(stck[top-1],stck[top],p[i])<0) top--; stck[++top]=p[i]; } top+=1; return ;}void solve(){ int max=0; int s; int i,j; for(i=0;i<top;i++) for(j=i+1;j<top;j++) { s=dis(stck[i],stck[j]); if(s>max) max=s; } printf("%d\n",max);}int main(){ // freopen("a.txt","r",stdin); while(scanf("%d",&n)!=EOF) { int i,j,k; for(i=0;i<n;i++) scanf("%d %d",&p[i].x,&p[i].y); if(n<3) { printf("%d\n",dis(p[0],p[1])); continue; } tubao(); solve(); } return 0;}
- poj 2187【凸包求最远距离】
- POJ 2187 Beauty Contest(凸包求最远点距离)
- POJ 2187 Beauty Contest(凸包:最远点对距离)
- poj 2187 求凸包最远两点距离的平方值
- POJ 2187 Beauty Contest(凸包:最远点对距离)
- POJ 2926 Requirements【最远曼哈顿距离】
- [poj 2926]Requirements[最远曼哈顿距离]
- poj管道光线的最远距离
- POJ.2926 平面最远哈密顿距离
- 最远距离
- 最远距离
- 最远距离
- 最远距离
- 最远距离
- POJ 2187 凸包的最远欧几里得距离:旋转卡壳算法
- POJ 2187:Beauty Contest 求给定一些点集里最远的两个点距离
- poj 2926 Requirements N维最远曼哈顿距离
- 最远曼哈顿距离小结 poj 2926 Requirements&hdu 4666 Hyperspace
- java duck的屏幕保护程序
- 过滤器做单点登录(包括后登录用户将之前用户挤出,防止url键入操作,已登录用户重操作其他不用再次输入用户名和密码)
- 带历史信息的菜单
- 项目笔记1:项目导入的问题以及SVN安装
- [C++] 如何不用虚函数实现多态
- poj 2187【凸包求最远距离】
- 《算法导论》学习总结——第二部分1堆排序
- SCP协议
- 解析TS的PSI信息
- C++ 仿函数(functor)
- Cannot open source file: 'C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC\APPMODUL.CPP': No su
- CSS打造导航菜单
- CentOS6 sudo: (whatever): command not found
- C++/STL/VC资源链接(查找方便)