这个题目没什么好说的。昨天做这个题目的时候,卷包裹法有很多心酸,死活过不去,当步进法就能够1A。
看Discuss里有人用旋转卡壳来写。也就学习了一下。这次也用旋转卡壳来写了一下。用凸包(两种方法)+旋转卡壳,都过了~~~!#¥%……&×()
步进法+旋转卡壳
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define EPS 1e-8
#define N 50002
struct point
{
double x;
double y;
}p[N];
int s[N];
int top;
int dblcmp(double d)
{
if(fabs(d)<EPS)return 0;
return d>0?1:-1;
}
double dis(point a,point b)
{
return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double det(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
double cross(point a,point b,point c)
{
return det(a.x-c.x,a.y-c.y,b.x-c.x,b.y-c.y);
}
bool cmp(point a,point b)
{
if(a.y==b.y)return a.x<b.x;
return a.y<b.y;
}
void graham_scan(int n)
{
sort(p,p+n,cmp);
top=1;
s[0]=0;
s[1]=1;
for(int i=2;i<n;i++)
{
while(top&&cross(p[s[top]],p[i],p[s[top-1]])<=0)
{
top--;
}
s[++top]=i;
}
int len=top;
s[++top]=n-2;
for(int i=n-3;i>=0;i--)
{
while(top!=len&&cross(p[s[top]],p[i],p[s[top-1]])<=0)
{
top--;
}
s[++top]=i;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
if(n==2)
{
printf("%.0lf\n",dis(p[0],p[1]));
continue;
}
graham_scan(n);
int q=1;
double ans=0;
for(int i=0;i<top;i++) //旋转卡壳
{
while(cross(p[s[i]],p[s[i+1]],p[s[q+1]])>cross(p[s[i]],p[s[i+1]],p[s[q]]))
{
q=(q+1)%top;
}
ans=max(ans,max(dis(p[s[i]],p[s[q]]),dis(p[s[i+1]],p[s[q+1]])));
}
printf("%.0lf\n",ans);
}
return 0;
}
卷包裹法+旋转卡壳
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define EPS 1e-8
#define N 50002
struct point
{
double x;
double y;
}p[N];
int s[N];
int top;
int dblcmp(double d)
{
if(fabs(d)<EPS)return 0;
return d>0?1:-1;
}
double dis(point a,point b)
{
return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double det(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
double cross(point a,point b,point c)
{
return det(a.x-c.x,a.y-c.y,b.x-c.x,b.y-c.y);
}
bool cmp1(point a,point b)
{
if(a.y==b.y)return a.x<b.x;
return a.y<b.y;
}
bool cmp2(point a,point b)
{
double d=cross(a,b,p[0]);
if(d>0)return true;
else if(d<0)return false;
else
{
double dis1=dis(a,p[0]);
double dis2=dis(b,p[0]);
if(dis1<dis2)return true;
else return false;
}
}
void graham_scan(int n)
{
sort(p,p+n,cmp1);
sort(p+1,p+n,cmp2);
top=-1;
s[++top]=0;
s[++top]=1;
s[++top]=2;
for(int i=3;i<n;i++)
{
while(top>=1&&dblcmp(cross(p[s[top]],p[i],p[s[top-1]]))<=0)
{
top--;
}
s[++top]=i;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
if(n==2)
{
printf("%.0lf\n",dis(p[0],p[1]));
continue;
}
graham_scan(n);
top++;
s[top]=s[0];
int q=1;
double ans=0;
for(int i=0;i<top;i++) //旋转卡壳
{
while(cross(p[s[i]],p[s[i+1]],p[s[q+1]])>cross(p[s[i]],p[s[i+1]],p[s[q]]))
{
q=(q+1)%top;
}
ans=max(ans,max(dis(p[s[i]],p[s[q]]),dis(p[s[i+1]],p[s[q+1]])));
}
printf("%.0lf\n",ans);
}
return 0;
}
凸包+枚举
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 50005
struct point
{
double x,y;
}p[N];
int s[N],top;
double dis(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
double det(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
double cross(point a,point b,point c)
{
return det(a.x-c.x,a.y-c.y,b.x-c.x,b.y-c.y);
}
bool cmp(point a,point b)
{
if(a.y==b.y)return a.x<b.x;
return a.y<b.y;
}
void graham_scan(int n)
{
sort(p,p+n,cmp);
top=1;
s[0]=0;
s[1]=1;
for(int i=2;i<n;i++)
{
while(top&&cross(p[s[top]],p[i],p[s[top-1]])<=0)
{
top--;
}
s[++top]=i;
}
int len=top;
s[++top]=n-2;
for(int i=n-3;i>=0;i--)
{
while(top!=len&&cross(p[s[top]],p[i],p[s[top-1]])<=0)
{
top--;
}
s[++top]=i;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
if(n==2)
{
printf("%.0lf\n",dis(p[0],p[1]));
continue;
}
else
{
graham_scan(n);
double dismax=-2147483646;
for(int i=0;i<top;i++)
{
for(int j=i+1;j<=top;j++)
{
if(dismax<dis(p[s[i]],p[s[j]]))
{
dismax=dis(p[s[i]],p[s[j]]);
}
}
}
printf("%.0lf\n",dismax);
}
}
return 0;
}