poj 2187 Beauty Contest

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 34928 Accepted: 10818

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个房子(2<=N<=50000),问最远的房子相距多少距离。

思路:

这就是旋转卡壳的一个最基础的应用,通过寻找所有的对踵点,找出最远点对(最远点对距离的平方)。

graham求凸包和旋转卡壳都可以做,graham时间复杂度也不高。

示例程序

graham求凸包:
Source Code

Problem: 2187		Code Length: 1447B
Memory: 784K		Time: 94MS
Language: G++		Result: Accepted
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct point
{
    int x,y;
}p[50000],tubao[50000],t;					//tubao[]记录凸包的定点
int dis(struct point a,struct point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cross(struct point a,struct point b,struct point o)
{
    return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
}
int cmp(struct point a,struct point b)
{
    if(cross(a,b,p[0])>0)
    {
        return 1;
    }
    else if(cross(a,b,p[0])==0&&dis(a,p[0])<dis(b,p[0]))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int gra(int n)
{
    int i,pos=0,top=2;
    for(i=1;n>i;i++)
    {
        if(p[i].y<p[pos].y||(p[i].y==p[pos].y&&p[i].x<p[pos].x))
        {
            pos=i;
        }
    }
    t=p[0];
    p[0]=p[pos];
    p[pos]=t;
    sort(p+1,p+n,cmp);					//以极坐标排序
    tubao[0]=p[0];
    tubao[1]=p[1];
    tubao[2]=p[2];
    for(i=3;n>i;i++)
    {
        while(top>1&&cross(p[i],tubao[top],tubao[top-1])>=0)
        {
            top--;
        }
        top++;
        tubao[top]=p[i];
    }
    return top;
}
int main()
{
    int n,i,i1,top,maxd=-1,d;
    scanf("%d",&n);
    for(i=0;n>i;i++)
    {
        scanf("%d %d",&p[i].x,&p[i].y);
    }
    top=gra(n);					//求凸包
    for(i=0;top>=i;i++)
    {
        for(i1=i+1;top>=i1;i1++)
        {
            d=dis(tubao[i],tubao[i1]);				//计算距离
            if(maxd<d)
            {
                maxd=d;
            }
        }
    }
    printf("%d",maxd);
    return 0;
}

旋转卡壳(O(n)更慢了,是书上代码不对还是有其他原因):

Source Code

Problem: 2187		Code Length: 2048B
Memory: 784K		Time: 110MS(94MS)			//括号里为O(n^2)算法所用时间
Language: G++		Result: Accepted
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct point
{
    int x,y;
}p[50000],ch[50000];
int det(int x1,int y1,int x2,int y2)
{
    return x1*y2-x2*y1;
}
int side(struct point a,struct point b,struct point p)
{
    return det(b.x-a.x,b.y-a.y,p.x-a.x,p.y-a.y);
}
int square_dis(struct point a,struct point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cmp(struct point a,struct point b)
{
    if(a.y<b.y)
    {
        return 1;
    }
    else if(a.y==b.y&&a.x<b.x)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int convex_hull(int n)
{
    int i,ix,t;
    sort(p,p+n,cmp);
    ch[0]=p[0];
    if(n==1)
    {
        ch[1]=ch[0];
        return 1;
    }
    ch[1]=p[1];
    if(n==2)
    {
        ch[2]=ch[0];
        return 2;
    }
    ix=2;
    for(i=2;n>i;i++)
    {
        while(ix>1&&side(ch[ix-2],ch[ix-1],p[i])<=0)                   //<=0 for no more than 3 points in a line
        {
            ix--;
        }
        ch[ix]=p[i];
        ix++;
    }
    t=ix;
    ch[ix]=p[n-2];
    ix++;
    for(i=n-3;i>=0;i--)
    {
        while(ix>t&&side(ch[ix-2],ch[ix-1],p[i])<=0)
        {
            ix--;
        }
        ch[ix]=p[i];
        ix++;
    }
    return ix-1;
}
//O(n^2)
/*int dia_rotating_calipers(int n)
{
    int i,j,t,dia=0;
    for(i=0;n>i;i++)
    {
        for(j=0;n>j;j++)
        {
            t=square_dis(ch[i],ch[j]);
            if(t>dia)
            {
                dia=t;
            }
        }
    }
    return dia;
}*/
//O(n)
int dia_rotating_calipers(int n)
{
    int i,q=1,dia=0;
    for(i=0;n>i;i++)
    {
        while(side(ch[i],ch[i+1],ch[q+1])>side(ch[i],ch[i+1],ch[q]))
        {
            q=(q+1)%n;
        }
        dia=max(dia,max(square_dis(ch[i],ch[q]),square_dis(ch[i+1],ch[q])));
    }
    return dia;
}
int main()
{
    int i,n,cn;
    scanf("%d",&n);
    for(i=0;n>i;i++)
    {
        scanf("%d %d",&p[i].x,&p[i].y);
    }
    cn=convex_hull(n);
    printf("%d",dia_rotating_calipers(cn));
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值