1. 程式人生 > >poj 2187【旋轉卡殼】

poj 2187【旋轉卡殼】

getch getchar() Go char tor pre -a str clu

求平面最遠點對

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=50005;
int n,w=1,top;
double ans;
struct dian 
{
    double x,y;
    dian(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
    dian operator + (const dian &a) const
{ return dian(x+a.x,y+a.y); } dian operator - (const dian &a) const { return dian(x-a.x,y-a.y); } }p[N],s[N]; int read() { int r=0,f=1; char p=getchar(); while(p>‘9‘||p<‘0‘) { if(p==‘-‘) f=-1; p=getchar(); } while
(p>=‘0‘&&p<=‘9‘) { r=r*10+p-48; p=getchar(); } return r*f; } double dis(dian a,dian b) { return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } double cj(dian a,dian b) { return a.x*b.y-a.y*b.x; } bool cmp(const dian &a,const dian &b) { int ar=cj(a-p[1
],b-p[1]); return ar>0||(ar==0&&a.x<b.x); } double mj(dian a,dian b,dian c) { return cj(b-a,c-a); } int main() { n=read(); for(int i=1;i<=n;i++) { int x=read(),y=read(); p[i]=dian(x,y); if(p[i].x<p[w].x||(p[i].x==p[w].x&&p[i].y<p[w].y)) w=i; } swap(p[w],p[1]); sort(p+2,p+1+n,cmp); s[++top]=p[1],s[++top]=p[2]; for(int i=3;i<=n;i++) { while(top>1&&cj(s[top]-p[i],s[top-1]-p[i])>=0) top--; s[++top]=p[i]; } w=2;s[top+1]=s[1]; for(int i=1;i<=top;i++) { while(cj((s[i]-s[w]),(s[i+1]-s[w]))<cj((s[i]-s[w+1]),(s[i+1]-s[w+1]))) w=(w+1)>top?1:w+1; ans=max(ans,dis(s[w],s[i])); } printf("%.0f\n",ans); return 0; }

poj 2187【旋轉卡殼】