poj2187(最远点的距离的平方)

7 篇文章 0 订阅
6 篇文章 0 订阅

题意:

给出n个点,求最远的点对的距离的平方。


思路:

旋转卡壳。


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;

const double pi=acos(-1.0);

const double eps=1e-8;

int cmp(double x)
{
	if(fabs(x)<eps)
		return 0;
	if(x>0)
		return 1;
	return -1;
}

inline double sqr(double x)
{
	return x*x;
}

struct point
{
	double x,y;
	point() {}
	point(double a,double b):x(a),y(b){}
	friend point operator + (const point &a,const point &b)
	{
		return point(a.x+b.x,a.y+b.y);
	}
	friend point operator - (const point &a,const point &b)
	{
		return point(a.x-b.x,a.y-b.y);
	}
	friend bool operator == (const point &a,const point &b)
	{
		return cmp(a.x-b.x)==0&&cmp(a.y-b.y)==0;
	}
	friend point operator * (const point &a,const double &b)
	{
		return point(a.x*b,a.y*b);
	}
	friend point operator * (const double &a,const point &b)
	{
		return point(a*b.x,a*b.y);
	}
	friend point operator / (const point &a,const double &b)
	{
		return point(a.x/b,a.y/b);
	}
	double norm()
	{
		return sqrt(sqr(x)+sqr(y));
	}
};

double det(const point &a,const point &b)//cha
{
	return a.x*b.y-a.y*b.x;
}
double dot(const point &a,const point &b)
{
	return a.x*b.x+a.y*b.y;
}
double dist(const point &a,const point &b)
{
	return (a-b).norm();
}
point rotate_point(const point &p,double A)//ni
{
	double tx=p.x,ty=p.y;
	return point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A));
}

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

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

polygon_convex convex_hull(vector<point> a)
{
	polygon_convex res(2*a.size()+5);
	sort(a.begin(),a.end(),cmp_less);
	a.erase(unique(a.begin(),a.end()),a.end());
	int m=0;
	for(int i=0;i<a.size();i++)
	{
		while(m>1 && cmp(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=int(a.size())-2;i>=0;i--)
	{
		while(m>k && cmp(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;
}

double convex_diameter(polygon_convex &a,int &First,int &Second)
{
	vector<point>  &p=a.P;
	int n=p.size();
	double maxd=0.0;
	if(n==1)
	{
		First=Second=0;
		return maxd;
	}
	#define next(i) ((i+1)%n)
	for(int i=0,j=1;i<n;i++)
	{
		while(cmp(det(p[next(i)]-p[i],p[j]-p[i])-det(p[next(i)]-p[i],p[next(j)]-p[i]))<0)
		{
			j=next(j);
		}
		double d=dist(p[i],p[j]);
		if(d>maxd)
		{
			maxd=d;
			First=i,Second=j;
		}
		d=dist(p[next(i)],p[next(j)]);
		if(d>maxd)
		{
			maxd=d;
			First=i,Second=j;
		}
	}
	return maxd;
}

vector<point> tt;
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		tt.clear();
		for(int i=0;i<n;i++)
		{
			double tx,ty;
			scanf("%lf%lf",&tx,&ty);
			tt.push_back(point(tx,ty));
		}
		polygon_convex xx=convex_hull(tt);
		int ans1,ans2;
		double ans=convex_diameter(xx,ans1,ans2);
		printf("%.0lf\n",ans*ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值