Poj2187 凸包求最大距离
2017-07-18 15:26
417 查看
题目链接:点击打开链接
凸包+暴力求解,注意n==0,和n==1的情况;
2.用旋转卡壳
用模板的写法(比赛更好code)
#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=5e4+10;
struct Point//点 向量
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
};
typedef Point Vector;
//向量使用点作为表示方法 结构相同 为了代码清晰定义宏加以区别
const double eps = 1e-8;
int dcmp(double x) //三态函数 处理与double零有关的精度问题
{
if(fabs(x) < eps) return 0;
return x<0 ? -1 : 1;
}
//向量运算
Vector operator + (Vector A, Vector B)
{
return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (Vector A, Vector B)
{
return Vector(A.x-B.x, A.y-B.y);
}
Vector operator * (Vector A, double p)
{
return Vector(A.x*p, A.y*p);
}
Vector operator / (Vector A, double p)
{
return Vector(A.x/p, A.y/p);
}
bool operator == (const Vector& A, const Vector& B)
{
return dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0;
}
bool operator < (const Point&a,const Point &b)
{
return a.x<b.x||(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;
}
double Dis(Point A, Point B)//两点距离
{
return sqrt((A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y));
}
int Area(Point a,Point b,Point c) //三角形面积||判断两直线的方向
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int Dis2(Point a,Point b)
{
return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int ConvexHull(Point p[],int n,Point ch[])
{
sort(p,p+n);
int m = 0;
for(int i = 0; i <n; i++)
{
while(m > 1 && dcmp(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 && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) m--;
ch[m++] = p[i];
}
if(n > 1) m--;
return m;
}
void RC(Point ch[],int n)
{
int l=n;
int q=1;
int ans=0;
ch
=ch[0];
for(int p=0;p<l;p++)
{
while(Area(ch[p+1],ch[q+1],ch[p])>Area(ch[p+1],ch[q],ch[p]))
q=(q+1)%l;
ans=max(ans,max(Dis2(ch[p],ch[q]),Dis2(ch[p+1],ch[q+1])));
}
printf("%d\n",ans);
}
int main()
{
Point p[maxn],ch[maxn];
int n,j,i;
while(~scanf("%d",&n))
{
for(i=0;i<n;i++)
scanf("%lf %lf",&p[i].x,&p[i].y);
if(n==0)printf("0\n");
else if(n==1)printf("%d\n",Dis2(p[0],p[1]));
else
{
int cn=ConvexHull(p,n,ch);
RC(ch,cn);
}
}
return 0;
}
凸包+暴力求解,注意n==0,和n==1的情况;
#include <iostream> #include<algorithm> #include<cmath> #include<stack> #include<vector> #include<cmath> #include<stdio.h> #define maxn 50000+10 using namespace std; typedef struct point { int x,y; } point; int n; point map[maxn],p; double dis(point p1,point p2)// 求两点的距离 { return sqrt((double)((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))); } int cross(point a,point b,point c) //三角形面积||判断两直线的方向 { return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); } bool cmp(point p1,point p2) { if(cross(map[0],p1,p2)==0) return dis(p1,map[0])<=dis(p2,map[0]); if(cross(map[0],p1,p2)>0)return true; return false; } void sort_cir() { int i; for(i=1; i<n; i++) if(map[i].y<map[0].y||(map[i].y==map[0].y&&map[i].x<map[0].x)) { point temp; temp=map[0]; map[0]=map[i]; map[i]=temp; } sort(map+1,map+n,cmp); } int di(point a,point b) { return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int muli(point p1,point p2,point p3) { point a,b; a.x=p1.x-p2.x; a.y=p1.y-p2.y; b.x=p2.x-p3.x; b.y=p2.y-p3.y; return b.x*a.y-b.y*a.x; } void tu() { int i; vector<point> s; s.empty(); for(i=0; i<2; i++) s.push_back(map[i]); for(i=2; i<n; i++) { while(muli(map[i],s[s.size()-1],s[s.size()-2])<=0) { s.pop_back(); } s.push_back(map[i]); } int Max=0; if(s.size()==1)cout<<"0"<<endl; else if(s.size()==2)cout<<di(s[0],s[1])<<endl; else { for(i=0;i<s.size();i++) for(int j=i+1;j<s.size();j++) Max=max(Max,di(s[i],s[j])); cout<<Max<<endl; } } int main() { int i; while(cin>>n&&n) { for(i=0; i<n; i++)cin>>map[i].x>>map[i].y; if(n==0)cout<<"0"<<endl; else if(n==1)cout<<di(map[0],map[1])<<endl; else { sort_cir(); tu(); } } return 0; }
2.用旋转卡壳
#include <iostream> #include<algorithm> #include<cmath> #include<stack> #include<vector> #include<cmath> #include<stdio.h> #define maxn 50000+10 using namespace std; typedef struct point { int x,y; } point; int n; point map[maxn],p; double dis(point p1,point p2)// 求两点的距离 { return sqrt((double)((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))); } int cross(point a,point b,point c) //三角形面积||判断两直线的方向 { return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); } int dis2(point a,point b) { return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int muli(point p1,point p2,point p3) { point a,b; a.x=p1.x-p2.x; a.y=p1.y-p2.y; b.x=p2.x-p3.x; b.y=p2.y-p3.y; return b.x*a.y-b.y*a.x; } bool cmp(point p1,point p2) { if(cross(map[0],p1,p2)==0) return dis(p1,map[0])<=dis(p2,map[0]); if(cross(map[0],p1,p2)>0)return true; return false; } vector<point> s; void Graham() { int i; for(i=1; i<n; i++) if(map[i].y<map[0].y||(map[i].y==map[0].y&&map[i].x<map[0].x)) { point temp; temp=map[0]; map[0]=map[i]; map[i]=temp; } sort(map+1,map+n,cmp); s.empty(); for(i=0; i<2; i++) s.push_back(map[i]); for(i=2; i<n; i++) { while(muli(map[i],s[s.size()-1],s[s.size()-2])<=0) { s.pop_back(); } s.push_back(map[i]); } } void RC() { int l=s.size(); int q=1; int ans=0; s.push_back(s[0]); for(int p=0;p<l;p++) { while(cross(s[p+1],s[q+1],s[p])>cross(s[p+1],s[q],s[p])) q=(q+1)%l; ans=max(ans,max(dis2(s[p],s[q]),dis2(s[p+1],s[q+1]))); } cout<<ans<<endl; } int main() { int i; while(cin>>n&&n) { for(i=0; i<n; i++)cin>>map[i].x>>map[i].y; if(n==0)cout<<"0"<<endl; else if(n==1)cout<<dis2(map[0],map[1])<<endl; else { Graham(); RC(); } } return 0; }
用模板的写法(比赛更好code)
#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=5e4+10;
struct Point//点 向量
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
};
typedef Point Vector;
//向量使用点作为表示方法 结构相同 为了代码清晰定义宏加以区别
const double eps = 1e-8;
int dcmp(double x) //三态函数 处理与double零有关的精度问题
{
if(fabs(x) < eps) return 0;
return x<0 ? -1 : 1;
}
//向量运算
Vector operator + (Vector A, Vector B)
{
return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (Vector A, Vector B)
{
return Vector(A.x-B.x, A.y-B.y);
}
Vector operator * (Vector A, double p)
{
return Vector(A.x*p, A.y*p);
}
Vector operator / (Vector A, double p)
{
return Vector(A.x/p, A.y/p);
}
bool operator == (const Vector& A, const Vector& B)
{
return dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0;
}
bool operator < (const Point&a,const Point &b)
{
return a.x<b.x||(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;
}
double Dis(Point A, Point B)//两点距离
{
return sqrt((A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y));
}
int Area(Point a,Point b,Point c) //三角形面积||判断两直线的方向
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int Dis2(Point a,Point b)
{
return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int ConvexHull(Point p[],int n,Point ch[])
{
sort(p,p+n);
int m = 0;
for(int i = 0; i <n; i++)
{
while(m > 1 && dcmp(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 && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) m--;
ch[m++] = p[i];
}
if(n > 1) m--;
return m;
}
void RC(Point ch[],int n)
{
int l=n;
int q=1;
int ans=0;
ch
=ch[0];
for(int p=0;p<l;p++)
{
while(Area(ch[p+1],ch[q+1],ch[p])>Area(ch[p+1],ch[q],ch[p]))
q=(q+1)%l;
ans=max(ans,max(Dis2(ch[p],ch[q]),Dis2(ch[p+1],ch[q+1])));
}
printf("%d\n",ans);
}
int main()
{
Point p[maxn],ch[maxn];
int n,j,i;
while(~scanf("%d",&n))
{
for(i=0;i<n;i++)
scanf("%lf %lf",&p[i].x,&p[i].y);
if(n==0)printf("0\n");
else if(n==1)printf("%d\n",Dis2(p[0],p[1]));
else
{
int cn=ConvexHull(p,n,ch);
RC(ch,cn);
}
}
return 0;
}
相关文章推荐
- poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
- poj 2187 凸包+平面上点之间最大距离
- poj 2187 N个点中输出2点的最大距离的平方
- POJ 2187 Beauty Contest(凸包求最远点距离)
- POJ 2187 Beauty Contest(凸包:最远点对距离)
- POJ 2187 求凸包上最长距离
- 【计算几何】 Andrew凸包算法 + 旋转卡壳(以求点对最长距离为例) -- 以 POJ 2187 Beauty Contest 为例
- poj 2187【凸包求最远距离】
- POJ2187 n个点找距离最远的点对距离 graham 凸包
- POJ 2187 凸包的最远欧几里得距离:旋转卡壳算法
- poj2187 凸包上的最远点对的距离(凸包+旋转卡壳)
- poj 2187 凸包 最大点对距
- poj 2187 求凸包最远两点距离的平方值
- poj2187-Beauty Contest 求凸包最远距离(旋转卡壳算法模板题)
- POJ 2187(求凸包内两点之间最长距离)
- POJ 2187 Beauty Contest(凸包:最远点对距离)
- POJ训练计划2187_Beauty Contest(几何/凸包)
- poj 1584 计算几何 (点到线段距离+判断点是否在多边形内+判断多边形是否为凸包)
- POJ 2187 Beauty Contest(凸包优化 || 凸包+旋转卡壳)
- poj 3608 Bridge Across Islands(两凸包最近距离)