题目:http://poj.org/problem?id=2187
学习资料:https://blog.csdn.net/wang_heng199/article/details/74477738
https://www.jianshu.com/p/74c25c0772d6
注意求凸包时先下后上,保持逆时针;
别忘了给点排序囧。
代码如下:
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int const xn=50005; int n,tot,ans; struct P{ int x,y; P(int x=0,int y=0):x(x),y(y) {} bool operator < (const P &b) const {return x<b.x||(x==b.x&&y<b.y);} }p[xn],c[xn]; P operator - (const P &a,const P &b){return P(a.x-b.x,a.y-b.y);} int cross(P a,P b){return a.x*b.y-a.y*b.x;} int maxx(int x,int y){return x<y?y:x;} int rd() { int ret=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();} while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar(); return f?ret:-ret; } void find() { sort(p+1,p+n+1);//! for(int i=1;i<=n;i++)//under { while(tot>1&&cross(c[tot]-c[tot-1],p[i]-c[tot])<=0)tot--; c[++tot]=p[i]; } int num=tot; for(int i=n-1;i;i--)//top { while(tot>num&&cross(c[tot]-c[tot-1],p[i]-c[tot])<=0)tot--; c[++tot]=p[i]; } tot--;//p[1] } int sqr(int x){return x*x;} int disq(P a,P b){return sqr(a.x-b.x)+sqr(a.y-b.y);} void work() { ans=disq(c[1],c[2]); c[tot+1]=c[1]; for(int i=1,p=2;i<=tot;i++) { while(cross(c[p]-c[i],c[p]-c[i+1])<cross(c[p+1]-c[i],c[p+1]-c[i+1])) {p++; if(p==tot+1)p=1;} ans=maxx(ans,maxx(disq(c[p],c[i]),disq(c[p+1],c[i+1]))); } } int main() { n=rd(); for(int i=1;i<=n;i++)p[i].x=rd(),p[i].y=rd(); find(); work(); printf("%d ",ans); return 0; }