POJ 2187 Beauty Contest(凸包:最远点对距离)
http://poj.org/problem?id=2187
题意:
平面上给你n个点,要你求出这n个点中的任意两点的最远距离的平方?
分析:
点集的最远点对一定是在凸包上的两个顶点,本题先求出点集的凸包,然后暴力枚举凸包上任意两个顶点的距离即可.(不会超时)本来用旋转卡壳应该是最好的,但是还没有学,只能暴力枚举了…
本题所有数据都是int,最后结果也只要返回距离的平方就行.这明摆着再说”如果全都用int计算,能加快计算速度”. 当然我还是用double做的.
还要注意凸包退化成2点的情况.
AC代码:
用C++提交,G++提交有double精度问题,会出错
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double eps=1e-10;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
return x<0?-1:1;
}
struct Point
{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
bool operator<(const Point &B)const
{
return dcmp(x-B.x)<0 || (dcmp(x-B.x)==0 && dcmp(y-B.y)<0);
}
bool operator==(const Point &B)const
{
return dcmp(x-B.x)==0 && dcmp(y-B.y)==0;
}
};
typedef Point Vector;
Vector operator-(Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
int ConvexHull(Point *p,int n,Point *ch)
{
sort(p,p+n);
n=unique(p,p+n)-p;
int m=0;
for(int i=0;i<n;i++)
{
while(m>1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])<=0) m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2;i>=0;i--)
{
while(m>k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])<=0 ) m--;
ch[m++]=p[i];
}
if(n>1) m--;
return m;
}
const int maxn=50000+5;
Point p[maxn],ch[maxn];
double dist2(Point a,Point b)//返回距离的平方
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int main()
{
int n;
while(scanf("%d",&n)==1)
{
for(int i=0;i<n;++i)
scanf("%lf%lf",&p[i].x,&p[i].y);
int m=ConvexHull(p,n,ch);
if(m==2)
{
printf("%.0lf",dist2(ch[0],ch[1]));
continue;
}
else
{
double ans=-1e10;
for(int i=0;i<m;i++)
for(int j=i+1;j<m;j++)
ans=max(ans,dist2(ch[i],ch[j]));
printf("%.0lf\n",ans);//要用C++提交,G++有精度问题
}
}
return 0;
}
分享到:
相关推荐
北大POJ2187-Beauty Contest 解题报告+AC代码
O(nlogn)凸包问题 poj2187
NULL 博文链接:https://128kj.iteye.com/blog/1749213
北大POJ1113-Wall【凸包】 解题报告+AC代码
NULL 博文链接:https://128kj.iteye.com/blog/1748635
初学者练题开始------在POJ上(注:是百练) 初学者练题开始------在POJ上(注:是百练) 初学者练题开始------在POJ上(注:是百练)
POJ上的DP分类: 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈), 1742, 1887, 1926(马尔科夫矩阵,求...
POJ 1300 Door Man:无向图、欧拉定理、gets、sscanf
北大POJ初级题-数据结构:解题报告+AC代码
poj还是leetcode ProgrammingCompetionCareer 大学的竞赛生涯结束了,抽时间整理了一部分曾经在OJ刷过的题,还有一些找不到了,或者是因为杂物太多(除了代码以外还有题解的pdf、题目的测试数据等等),不想整理了就...
poj 2820 古代密码 http://poj.grids.cn/problem?id=2820 可直接运行
poj1002 source code input: The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on ...
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
poj上的测室数据,需要的下载........
网上整理的一些poj刷题指南。 poj地址:http://poj.org
poj 5346题,四则运算表达式求值的实现,用栈和队列实现中缀转后缀后计算,含解题报告与源码
poj1691解题报告 题目来源:http://acm.pku.edu.cn/JudgeOnline/showproblem?problem_id=1691(POJ No.1691) 解法: 搜索
poj与leetcode
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
北大POJ1009-Edge Detection 解题报告+AC代码