POJ2187 求平面上最远点对

最远点对一定是凸包上的点对,而对于随机数据,凸包上的点会比所有点少很多,所以虽然n

=50000,但是求出凸包以后直接枚举也是可过的

另外一种方法就是用旋转卡壳来求凸包直径

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define maxl 50010
#define eps 1e-8

using namespace std;

inline int sgn(double x)
{
	if(x>-eps && x<eps) return 0;
	if(x>0) return 1;
	else	return -1;
}
struct point 
{
	double x,y;
	point(double a=0,double b=0)
	{
		x=a,y=b;
	}
	inline point operator - (const point &b)const
	{
		return point(x-b.x,y-b.y);
	}
	inline bool operator == (const point &b)const
	{
		return sgn(x-b.x)==0 && sgn(y-b.y)==0;
	}
	inline double norm2()
	{
		return x*x+y*y;
	}
};
inline double dot(const point &a,const point &b)
{
	return a.x*b.x+a.y*b.y;
}
inline double det(const point &a,const point &b)
{
	return a.x*b.y-a.y*b.x;
}

struct polygon_convex
{
	vector<point> P;
	polygon_convex(int size=0)
	{
		P.resize(size);
	}
};

inline bool cmp(const point &a,const point &b)
{
	if(sgn(a.x-b.x)==0)
		return sgn(a.y-b.y)<0;
	return sgn(a.x-b.x)<0;
}

polygon_convex convex_hull(vector<point> a)
{
	polygon_convex res(2*a.size()+5);
	sort(a.begin(),a.end(),cmp);
	a.erase(unique(a.begin(),a.end()),a.end());
	int m=0,l=a.size();
	for(int i=0;i<l;i++)
	{
		while(m>1 && sgn(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)
			--m;
		res.P[m++]=a[i];
	}
	int k=m;
	for(int i=l-2;i>=0;i--)
	{
		while(m>k && sgn(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)
			--m;
		res.P[m++]=a[i];
	}
	res.P.resize(m);
	if(a.size()>1)
		res.P.resize(m-1);
	return res;
}

int n;
vector<point> a;
polygon_convex res;
double ans;

inline void prework()
{
	a.clear();
	point p;
	for(int i=1;i<=n;i++)
	{
		scanf("%lf%lf",&p.x,&p.y);
		a.push_back(p);
	}
}

inline double convex_diameter(polygon_convex &a,int &fir,int &sec)
{
	vector<point> &p=a.P;
	int n=p.size();
	double maxd=0.0;
	if(n==1)
	{
		fir=sec=0;
		return 0;
	}
	#define nxt(i) ((i+1)%n)
	for(int i=0,j=1;i<n;i++)
	{
		while(sgn(det(p[nxt(i)]-p[i],p[j]-p[i])-det(p[nxt(i)]-p[i],p[nxt(j)]-p[i]))<0)
			j=nxt(j);
		double d=(p[j]-p[i]).norm2();
		if(d>maxd)
		{
			maxd=d;
			fir=i;sec=j;
		}
		d=(p[nxt(j)]-p[nxt(i)]).norm2();
		if(d>maxd)
		{
			maxd=d;
			fir=nxt(i);sec=nxt(j);
		}
	}
	return maxd;
}

inline void mainwork()
{
	res=convex_hull(a);
	ans=0;int l=res.P.size();
/*	for(int i=0;i<l;i++)
		for(int j=0;j<l;j++)
			ans=max(ans,(res.P[i]-res.P[j]).norm2());
*/
	int x,y;
	ans=convex_diameter(res,x,y);
}

inline void print()
{
	printf("%.0f\n",ans);
}

int main()
{
	while(~scanf("%d",&n))
	{
		prework();
		mainwork();
		print();
	} 
	return 0;
} 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值