POJ2187->凸包
题意:
给出平面上n个点,求出这些点中距离最大的两个点。
题解:
暴力枚举凸包顶点:
如果逐个枚举,时间复杂度将会相当高,不能满足题目要求。
可以构建一个凸包,再在凸包中枚举各个顶点之间的距离求最大值。
代码:
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <float.h>
using namespace std ;
#define MAX 50005
const double eps = 1e-8 ;
int sgn(double x)
{
if(fabs(x) < eps) return 0 ;
if(x < 0) return -1 ;
else return 1 ;
}
struct Point
{
double x , y ;
Point(){}
Point(double _x , double _y)
{
x = _x ; y = _y ;
}
Point operator - (const Point &b) const
{
return Point(x - b.x , y - b.y) ;
}
double operator ^ (const Point &b) const
{
return x*b.y - y*b.x ;
}
double operator * (const Point &b) const
{
return x*b.x + y*b.y ;
}
};
struct Line
{
Point s , e ;
Line(){}
Line(Point _s , Point _e)
{
s = _s ; e = _e ;
}
};
double xmult(Point p0,Point p1,Point p2) //叉积p0p1 X p0p2
{
return (p1-p0)^(p2-p0);
}
double dist(Point a ,Point b)
{
return sqrt((b - a) * (b - a)) ;
}
double PointToLine(Point p ,Line L)
{
Point result ;
double t = ((p-L.s)*(L.e-L.s))/((L.e-L.s)*(L.e-L.s));
result.x=L.s.x+(L.e.x-L.s.x)*t ;
result.y=L.s.y+(L.e.y-L.s.y)*t ;
return dist(p , result) ;
}
Point list[MAX] ;
int Stack[MAX] , top ;
bool _cmp(Point p1,Point p2)
{
double tmp = (p1-list[0])^(p2-list[0]);
if(sgn(tmp) > 0)return true;
else if(sgn(tmp) == 0 && sgn(dist(p1,list[0]) - dist(p2,list[0])) <= 0)
return true;
else return false;
}
void anglesort(int n) //输入,并把最左下方的点放在list[0],并且进行极角排序
{
int i,k;
Point p0;
scanf("%lf%lf",&list[0].x,&list[0].y);
p0.x=list[0].x;
p0.y=list[0].y;
k=0;
for(i=1;i<n;i++)
{
scanf("%lf%lf",&list[i].x,&list[i].y);
if( (p0.y>list[i].y) || ((p0.y==list[i].y)&&(p0.x>list[i].x)) )
{
p0.x=list[i].x;
p0.y=list[i].y;
k=i;
}
}
list[k]=list[0];
list[0]=p0;
sort(list+1,list+n,_cmp);
}
void Graham(int n)
{
Point p0;
int k = 0;
p0 = list[0];
//找最下边的一个点
for(int i = 1;i < n;i++)
{
if( (p0.y > list[i].y) || (p0.y == list[i].y && p0.x > list[i].x) )
{
p0 = list[i];
k = i;
}
}
swap(list[k],list[0]);
sort(list+1,list+n,_cmp);
if(n == 1)
{
top = 1;
Stack[0] = 0;
return;
}
if(n == 2)
{
top = 2;
Stack[0] = 0;
Stack[1] = 1;
return ;
}
Stack[0] = 0;
Stack[1] = 1;
top = 2;
for(int i = 2;i < n;i++)
{
while(top > 1 && sgn((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]])) <= 0)
top--;
Stack[top++] = i;
}
}
double solve()
{
double ans = -1 ,temp;
ans = dist(list[Stack[0]] , list[Stack[top - 1]]) ;
for(int i = 0 ; i < top - 1 ; i ++)
{
for(int j = i + 1 ; j < top ; j ++)
{
temp = dist(list[Stack[i]] , list[Stack[j]]) ;
ans = max(ans , temp * temp) ;
}
}
return (ans) ;
}
int main()
{
int n , cas = 1 , len;
scanf("%d" , &n);
anglesort(n) ;
Graham(n) ;
printf("%.f\n",solve());
return 0 ;
}
旋转卡壳:
旋转卡壳介绍:
http://www.cnblogs.com/xdruid/archive/2012/07/01/2572303.html
http://blog.csdn.net/hanchengxi/article/details/8639476
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
struct Point
{
int x,y;
Point(int _x = 0, int _y = 0)
{
x = _x;
y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x, y - b.y);
}
int operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
int operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
void input()
{
scanf("%d%d",&x,&y);
}
};
int dist2(Point a,Point b)
{
return (a-b)*(a-b);
}
const int MAXN = 50010;
Point list[MAXN];
int Stack[MAXN],top;
bool _cmp(Point p1,Point p2)
{
int tmp = (p1-list[0])^(p2-list[0]);
if(tmp > 0)return true;
else if(tmp == 0 && dist2(p1,list[0]) <= dist2(p2,list[0]))
return true;
else return false;
}
void Graham(int n)
{
Point p0;
int k = 0;
p0 = list[0];
for(int i = 1;i < n;i++)
if(p0.y > list[i].y || (p0.y == list[i].y && p0.x > list[i].x))
{
p0 = list[i];
k = i;
}
swap(list[0],list[k]);
sort(list+1,list+n,_cmp);
if(n == 1)
{
top = 1;
Stack[0] = 0;
return;
}
if(n == 2)
{
top = 2;
Stack[0] = 0;
Stack[1] = 1;
return;
}
Stack[0] = 0;
Stack[1] = 1;
top = 2;
for(int i = 2;i < n;i++)
{
while(top > 1 && ((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]])) <= 0 )
top--;
Stack[top++] = i;
}
}
//旋转卡壳,求两点间距离平方的最大值
int rotating_calipers(Point p[],int n)
{
int ans = 0;
Point v;
int cur = 1;
for(int i = 0;i < n;i++)
{
v = p[i]-p[(i+1)%n];
while((v^(p[(cur+1)%n]-p[cur])) < 0)
cur = (cur+1)%n;
//printf("%d %d\n",i,cur);
ans = max(ans,max(dist2(p[i],p[cur]),dist2(p[(i+1)%n],p[(cur+1)%n])));
}
return ans;
}
Point p[MAXN];
int main()
{
int n;
while(scanf("%d",&n) == 1)
{
for(int i = 0;i < n;i++)
list[i].input();
Graham(n);
for(int i = 0;i < top;i++)
p[i] = list[Stack[i]];
printf("%d\n",rotating_calipers(p,top));
}
return 0;
}