POJ 2187-Beauty Contest(凸包-Graham扫描法/旋转卡壳法)

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 36053 Accepted: 11183

Description

Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates. 

Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms. 

Input

* Line 1: A single integer, N 

* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm 

Output

* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other. 

Sample Input

4
0 0
0 1
1 1
1 0

Sample Output

2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 

Source


题目意思:

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

解题思路:

先求凸包,然后枚举凸包上的所有点对并计算距离。
求凸包的时候,可以用Graham扫描法或者旋转卡壳法。

代码一:Graham扫描法
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 60050;
const int INF = 0x3f3f3f3f;

int EPS=1e-10;
int add(int a,int b)//int加法运算考虑误差
{
    if(abs(a+b)<EPS*(abs(a)+abs(b))) return 0;
    return a+b;
}
struct P//结构体定义二维向量
{
    int x,y;
    P() {}
    P(int x,int 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));
    }
    P operator *(int d)
    {
        return P(x*d,y*d);
    }
    int dot(P p)//内积
    {
        return add(x*p.x,y*p.y);
    }
    int det(P p)//外积
    {
        return add(x*p.y,-y*p.x);
    }
};
bool cmp(const P& p,const P& q)//字典序比较
{
    if(p.x!=q.x) return p.x<q.x;
    else return p.y<q.y;
}
vector<P> convex_hull(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 p,P q)//距离的平方
{
    return (p-q).dot(p-q);
}
int N;
P ps[maxn];
void solve()
{
    vector<P> qs=convex_hull(ps,N);
    double res=0;
    for(int i=0; i<qs.size(); ++i)
        for(int j=0; j<i; ++j)
            res=max(res,dist(qs[i],qs[j]));
    printf("%.0f\n",res);
}
int main()
{
    while (scanf("%d", &N) != EOF)
    {
        for(int i=0; i<N; ++i)
            scanf("%d%d",&ps[i].x,&ps[i].y);
        solve();
    }
    return 0;
}
/**
4
0 0
0 1
1 1
1 0
**/

代码二:旋转卡壳法
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 60050;
const int INF = 0x3f3f3f3f;

int EPS=1e-10;
int add(int a,int b)//int加法运算考虑误差
{
    if(abs(a+b)<EPS*(abs(a)+abs(b))) return 0;
    return a+b;
}
struct P//结构体定义二维向量
{
    int x,y;
    P() {}
    P(int x,int 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));
    }
    P operator *(int d)
    {
        return P(x*d,y*d);
    }
    int dot(P p)//内积
    {
        return add(x*p.x,y*p.y);
    }
    int det(P p)//外积
    {
        return add(x*p.y,-y*p.x);
    }
};
bool cmp(const P& p,const P& q)//字典序比较
{
    if(p.x!=q.x) return p.x<q.x;
    else return p.y<q.y;
}
vector<P> convex_hull(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 p,P q)//距离的平方
{
    return (p-q).dot(p-q);
}
int N;
P ps[maxn];
void solve()
{
    vector<P> qs = convex_hull(ps, N);
    int n=qs.size();
    if (n == 2)          //特别处理凸包退化的情况
    {
        printf("%.0f\n", dist(qs[0],qs[1]));
        return;
    }
    int i=0, j=0;           //某个方向上的对踵点对
    //求出x轴方向上的对踵点对
    for (int k=0; k<n; k++)
    {
        if (!cmp(qs[i],qs[k]))
            i=k;
        if (cmp(qs[j],qs[k]))
            j=k;
    }
    double res=0;
    int si=i, sj=j;
    while (i!=sj||j!=si)      //将方向逐步旋转180度
    {
        res=max(res, dist(qs[i], qs[j]));
        //判断先转到边i-(i+1)的法线方向还是边j-(j+1)的法线方向
        if ((qs[(i+1)%n]-qs[i]).det(qs[(j+1)%n]-qs[j])<0)
            i=(i+1)%n;        //先转到边i-(i+1)的法线方向
        else
            j=(j+1)%n;        //先转到边j-(j+1)的法线方向
    }
    printf("%.0f\n", res);
}
int main()
{
    while (scanf("%d", &N) != EOF)
    {
        for(int i=0; i<N; ++i)
            scanf("%d%d",&ps[i].x,&ps[i].y);
        solve();
    }
    return 0;
}
/**
4
0 0
0 1
1 1
1 0
**/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值