pku 2187(平面最远点对距离)

又要开始做题的日子了,找了道简单题练手。据说数据很水,直接枚举都可以过。

我本想凸包+旋转卡壳,但死活WA,最后只好用凸包+枚举。

l路过的大牛给点提示。

#include <iostream> using namespace std; int N,stackTop; struct node { int x,y; }stack[50005]; int Distance(node &a,node &b) { return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } int CrossMutiply(node& a,node& b,node& c) { return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); } int CMP(const void* a,const void* b) { node *c=(node*)a,*d=(node*)b; int m=CrossMutiply(stack[0],*c,*d); if(m==0) return Distance(*c,stack[0])-Distance(*d,stack[0]); else return -m; } void Convex() { qsort(stack+1,N-1,sizeof(node),CMP); stackTop=1; for(int i=2;i<N;i++) { while(stackTop>=1&&CrossMutiply(stack[stackTop-1],stack[stackTop],stack[i])<=0) stackTop--; stack[++stackTop]=stack[i]; } } int main() { scanf("%d",&N); for(int i=0;i<N;i++) { scanf("%d%d",&stack[i].x,&stack[i].y); if(stack[i].y<stack[0].y||(stack[i].y==stack[0].y&&stack[i].x<stack[0].x)) { node tmp=stack[0]; stack[0]=stack[i]; stack[i]=tmp; } } Convex(); int maxLength=0; for(int i=0;i<=stackTop;i++) { for(int j=i+1;j<=stackTop;j++) { if(Distance(stack[i],stack[j])>maxLength) maxLength=Distance(stack[i],stack[j]); } } /*int i=0,j=1; while( i <=stackTop ) { int d1 = Distance(stack[i],stack[j]); int d2 = Distance(stack[i],stack[j+1]); while(d1 < d2 && j + 1 <=stackTop) { j++; d1 = Distance(stack[i],stack[j]); d2 = Distance(stack[i],stack[j+1]); } if( maxLength < d1 ) maxLength = d1; i ++; }*/ printf("%d/n",maxLength); return 0; }

你可能感兴趣的:(pku 2187(平面最远点对距离))