题目大意:凸包最远点对
思路:模板套,初写很粗糙。好几个相同函数拼凑成的,写的很丑陋。。
program:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
struct point
{
int x,y;
}p[50005],res[50005];
double Dist(const point &arg1, const point &arg2)
{
return sqrt((double) ((arg1.x - arg2.x)*(arg1.x - arg2.x) + (arg1.y - arg2.y)*(arg1.y - arg2.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 dis(point a,point b)//距离
{
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
int multi(point p2,point p1,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int mysort1(point a,point b)
{
if(a.y!=b.y) return a.y<b.y;
if(a.y==b.y&&a.x!=b.x) return a.x<b.x;
}
bool cmp(const point &a,const point &b)
{
point temp=p[0];
double xmt=(a.x-temp.x)*(b.y-temp.y)-(b.x-temp.x)*(a.y-temp.y);
if(xmt)
return xmt>0;
return Dist(a,temp)<Dist(b,temp);
}
int rotating_calipers(int n)
{
int q=1;
int ans=0;
res[n]=res[0];
for(int p=0;p<n;p++)
{
while(abs(cross(res[p+1],res[q+1],res[p]))>abs(cross(res[p+1],res[q],res[p])))
q=(q+1)%n;
ans=max(ans,max(dis(res[p],res[q]),dis(res[p+1],res[q+1])));
}
return ans;
}
int main()
{
int n,k,len;
while(cin>>n)
{
for(int i=0;i<n;i++)
cin>>p[i].x>>p[i].y;
sort(p,p+n,mysort1);//排序,找到最左下角的点
res[0]=p[0];
sort(p+1,p+n,cmp);//按照极角排序
res[1]=p[1];
int top=1;
for(int i=2;i<n;i++)//i=2:好把共线的点弹出去
{
while( top && multi(p[i],res[top],res[top-1])<=0)
top--;
res[++top]=p[i];
}
//for(int i=0;i<=top;i++)
//cout<<res[i].x<<" "<<res[i].y<<endl;
cout<<rotating_calipers(top+1)<<endl; 凸包上的点数不是n个,注意了,改了就对了囧。
}
system("pause");
return 0;}