Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 19161 | Accepted: 5792 |
Description
Input
Output
Sample Input
4 0 0 0 1 1 1 1 0
Sample Output
2
Hint
Source
#include<cstdio> #include<cmath> #include<iostream> #include<algorithm> using namespace std; const int mm=55555; struct point { int x,y; } g[mm],q[mm]; int i,j,ans,top,n; int crotch(point a,point b) { return a.x*b.y-b.x*a.y; } int muilcro(point a,point b,point c) { return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y); } int sqrdis(point a,point b) { return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } bool cmp1(point a,point b) { return (a.x<b.x)||(a.x==b.x&&a.y<b.y); } bool cmp(point a,point b) { int k=muilcro(a,b,g[0]); if(k<0)return 1; if(k>0)return 0; return sqrdis(a,g[0])>=sqrdis(b,g[0]); } void Graham() { int i,j; for(i=j=0; i<n; ++i) if(g[i].x<g[j].x||(g[i].x==g[j].x&&g[i].y<g[j].y))j=i; q[0]=g[0],g[0]=g[j],g[j]=q[0]; sort(g+1,g+n+1,cmp); q[0]=g[0]; q[top=1]=g[1]; for(i=2; i<n; ++i) { while(top&&muilcro(q[top],g[i],q[top-1])>0)--top; q[++top]=g[i]; } } int main() { while(scanf("%d",&n)!=-1) { for(i=0; i<n; ++i) scanf("%d%d",&g[i].x,&g[i].y); Graham(); for(ans=i=0;i<top;++i) for(j=i+1;j<=top;++j) ans=max(ans,sqrdis(q[i],q[j])); printf("%d\n",ans); } return 0; }
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int mm=55555; typedef int diy; struct point { diy x,y; point(){} point(diy _x,diy _y):x(_x),y(_y){} }g[mm],q[mm]; point Vector(point s,point t) { return point(t.x-s.x,t.y-s.y); } diy CrossProduct(point P,point Q) { return P.x*Q.y-P.y*Q.x; } diy MultiCross(point P,point Q,point R) { return CrossProduct(Vector(Q,P),Vector(Q,R)); } diy SqrDis(point P,point Q) { return (P.x-Q.x)*(P.x-Q.x)+(P.y-Q.y)*(P.y-Q.y); } bool TurnRight(point P,point Q,point R) { diy tmp=MultiCross(P,Q,R); if(tmp>0)return 1; if(tmp<0)return 0; return SqrDis(P,Q)<SqrDis(P,R); } bool cmp(point P,point Q) { return TurnRight(g[0],Q,P); } void Graham(int n,int &m) { int i,j; for(j=i=0;i<n;++i) if(g[i].x<g[j].x||(g[i].x==g[j].x&&g[i].y<g[j].y))j=i; swap(g[0],g[j]); sort(g+1,g+n,cmp); q[m=0]=g[n]=g[0]; for(i=1;i<=n;++i) { while(m&&TurnRight(q[m-1],q[m],g[i]))--m; q[++m]=g[i]; } } diy RotatingCalipers(int n) { int l,r=1; diy ret=0; for(l=0;l<n;++l) { while(MultiCross(q[l+1],q[l],q[r])<MultiCross(q[l+1],q[l],q[r+1]))r=(r+1)%n; ret=max(ret,max(SqrDis(q[l],q[r]),SqrDis(q[l+1],q[r+1]))); } return ret; } int main() { int i,n,m; while(~scanf("%d",&n)) { for(i=0;i<n;++i) scanf("%d%d",&g[i].x,&g[i].y); Graham(n,m); printf("%d\n",RotatingCalipers(m)); } return 0; }