POJ 2187 Beauty Contest(计算几何)

题目链接:http://poj.org/problem?id=2187

题意:平面上有N个牧场,i号牧场的位置在格点(x,y),所有牧场的位置互不相同。请计算距离最远的两个牧场的距离,输出最远距离的平方

题解:裸凸包
先按x,y坐标升序排序
排序后的第一个和最后一个肯定是凸包上的点
它们之间可以分成上下两条链求解
在构造过程中加上新的点之后可能会破坏凸性,此时只要将凹的部分从末尾去除就好了

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <functional>
#define RI(N) scanf("%d",&(N))
#define RII(N,M) scanf("%d %d",&(N),&(M))
#define RIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
#define mem(a) memset((a),0,sizeof(a))
using namespace std;
const int inf=1e9;
const int inf1=-1*1e9;
typedef long long LL;
double EPS=1e-10;

double add(double a,double b)
{
    if(abs(a+b)<EPS*(abs(a)+abs(b))) return 0;
    else return a+b;

}
struct P
{
    double x,y;
    P() {}
    P(double x,double y) : x(x),y(y) {}

    P operator - (P p)
    {
        return P(add(x,-p.x),add(y,-p.y));
    }

    P operator + (P p)
    {
        return P(add(x,p.x),add(y,p.y));
    }
    double det(P p)
    {
        return x*p.y-y*p.x;
    }


};

P p[50005];
bool cmp(P x,P y)
{
    if(x.x==y.x) return x.y<y.y;
    else return x.x<y.x;
}

vector<P> convex(P *ps,int n)
{
    sort(ps,ps+n,cmp);
    int k=0;
    vector<P> qs(n*2);

    for(int i=0; i<n; i++)
    {
        while(k>1&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
        qs[k++]=ps[i];
    }

    for(int i=n-2,t=k; i>=0; i--)
    {
        while(k>t&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
        qs[k++]=ps[i];
    }
    qs.resize(k-1);
    return qs;

}
double dist(P p1,P p2)
{
    return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
}
int main()
{
    int n;
    RI(n);
    P p[50005];

    for(int i=0; i<n; i++)
        scanf("%lf %lf",&p[i].x,&p[i].y);
    vector<P> qs=convex(p,n);
    double res=-1;
    for(int i=0; i<qs.size(); i++)
        for(int j=i+1; j<qs.size(); j++)
        {
            res=max(res,dist(qs[i],qs[j]));
        }
    printf("%.0lf\n",res);




    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值