Poj2187 凸包求最大距离

题目链接:点击打开链接

凸包+暴力求解,注意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[n]=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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值