POJ 2187 Beauty Contest(凸包:最远点对距离)

POJ 2187 Beauty Contest(凸包:最远点对距离)

http://poj.org/problem?id=2187

题意:

       平面上给你n个点,要你求出这n个点中的任意两点的最远距离的平方?

分析:

       点集的最远点对一定是在凸包上的两个顶点,本题先求出点集的凸包,然后暴力枚举凸包上任意两个顶点的距离即可.(不会超时)本来用旋转卡壳应该是最好的,但是还没有学,只能暴力枚举了…

       本题所有数据都是int,最后结果也只要返回距离的平方就行.这明摆着再说”如果全都用int计算,能加快计算速度”. 当然我还是用double做的.(用int大概64ms,用double大概200ms,也差不多)

       还要注意凸包退化成2点的情况.

#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(y-B.y)==0;
    }
    bool operator<(const Point &B)const
    {
        return dcmp(x-B.x)<0 ||(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 Dot(Vector A,Vector B)
{
    return A.x*B.x+A.y*B.y;
}
double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}
bool InSegment(Point P,Point A,Point B)
{//A==B时也能正确运行
    return dcmp(Cross(A-P,B-P))==0 && dcmp(Dot(A-P,B-P))<=0;
}
bool SegmentIntersection(Point a1,Point a2,Point b1,Point b2)
{//a1==a2或b1==b2时,也能正确运行
    double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1);
    double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
    if(dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0) return true;
    if(dcmp(c1)==0 && InSegment(b1,a1,a2) ) return true;
    if(dcmp(c2)==0 && InSegment(b2,a1,a2) ) return true;
    if(dcmp(c3)==0 && InSegment(a1,b1,b2) ) return true;
    if(dcmp(c4)==0 && InSegment(a2,b1,b2) ) return true;
    return false;
}
bool PointInPolygon(Point p,Point *poly,int n)
{//poly点集大小==1或==2时也能正确运行
    int wn=0;
    for(int i=0;i<n;++i)
    {
        if(InSegment(p,poly[i],poly[(i+1)%n])) return true;
        int k=dcmp(Cross(poly[(i+1)%n]-poly[i], p-poly[i]));
        int d1=dcmp(poly[i].y-p.y);
        int d2=dcmp(poly[(i+1)%n].y-p.y);
        if(k>0 && d1<=0 && d2>0) wn++;
        if(k<0 && d2<=0 && d1>0) wn--;
    }
    if(wn!=0) return true;
    return false;
}
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;
double dist1(Point a ,Point b)
{
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
Point P[maxn];
int main()
{
    int n,m;
    while(scanf("%d",&n)==1 && n)
    {
        Point p[maxn],q[maxn];
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        m=ConvexHull(p,n,P);//计算两个凸包
        if(m==2)
		{
			printf("%.0lf",dist1(P[0],P[1]));
            continue;
		}
        else
		{
			double ans=-1;
			for(int i=0;i<m;i++)
				for(int j=i+1;j<m;j++)
			{
				ans=max(ans,dist1(P[i],P[j]));
			}
			printf("%.0lf",ans);
		}
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值