POJ 2187 Beauty Contest(凸包&&平面最远点对)

利用graham算法枚举凸包。

#include<map>
#include<cmath>
#include<queue>
#include<cmath>
#include<cstdio>
#include<stack>
#include<iostream>
#include<cstring>
#include<algorithm>
#define inf 999999999.9
#define eps 1e-8
#define ls l,mid,rt<<1
#define rs mid+1,rt,rt<<1|1
#define LL __int64
using namespace std;
struct P{
    int x,y;
}p[50005],s[50005];
int n,top;
int dis(P a,P b){
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

P operator-(P a,P b){
    P t;t.x=a.x-b.x;t.y=a.y-b.y;return t;
}

int operator*(P a,P b){
    return a.x*b.y-a.y*b.x;
}

bool operator<(P a,P b){
    int t=(a-p[1])*(b-p[1]);
    if(t==0)return dis(a,p[1])<dis(b,p[1]);
    return t>0;
}

void Graham(){
    int k = 1;
    for(int i = 2;i <= n;++ i ){
        if( (p[k].y > p[i].y)||(p[k].y == p[i].y && p[k].x > p[i].x) )
            k = i;
    }
    swap(p[1],p[k]);
    sort(p + 2,p + n + 1);
    s[1]=p[1];
    s[2]=p[2];top = 2;
    for(int i = 3;i <= n; ++ i){
        while(top > 1&&(p[i] - s[top - 1])*(s[top]-s[top-1]) >= 0)
            top--;
        s[++top] = p[i];
    }
}
int RC(){
    int q = 2,ans = 0;
    s[top+1 ]= p[1];
    for(int i = 1;i <= top;++ i){
        while((s[i+1]-s[i])*(s[q+1]-s[i])>(s[i+1]-s[i])*(s[q]-s[i]) )
            q = q%top+1;
        ans = max(ans,max(dis(s[q+1],s[i+1]),dis(s[q],s[i])));
    }
    return ans;
}
int main(){
    int j,i;
    while(~scanf("%d",&n)){
        for(int i = 1;i <= n;++ i){
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        Graham();
        printf("%d",RC());
    }
    return 0;
}

你可能感兴趣的:(凸包,计算几何,平面最远点对)