poj 2187 Beauty Contest(凸包)

原创 2016年05月31日 17:33:43
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

题意:有N个牧场,求最远两个牧场的距离的平方。

如果一个一个搜索肯定会超时,因此只需枚举凸包上的点就可以了。

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int n,top;
struct point
{
    int x,y;
} p[50005],s[50005],t;
int cross(point t1,point t2,point t3,point t4)  //求向量t1t2和向量t3t4的叉积
{
    return (t2.x-t1.x)*(t4.y-t3.y)-(t2.y-t1.y)*(t4.x-t3.x);
}
int dis1(point t1,point t2)   //求距离平方
{
   int z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
    return z;
}
double dis(point t1,point t2)   //求距离
{
    double z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
    return sqrt(z);
}
bool cmp(point t1,point t2)
{
    double z=cross(p[0],t1,p[0],t2);
    return z?z>0:dis(p[0],t1)>dis(p[0],t2);
}
void findpoint()    //找基点,按y从小到大,如果y相同,按x从左到右
{
    int i,j=0;
    t=p[0];
    for(i=1; i<n; i++)
    {
        if(t.y>p[i].y||(t.y==p[i].y&&t.x>p[i].x))
        {
            j=i;
            t=p[i];
        }
    }
    t=p[0];
    p[0]=p[j];
    p[j]=t;
}
void scanner()
{
    int i;
    findpoint();
    sort(p+1,p+n,cmp);
    s[0]=p[0];
    s[1]=p[1];
    top=1;
    for(i=2; i<n; i++)
    {
        while(cross(s[top-1],s[top],s[top],p[i])<0)
            top--;
        top++;
        s[top]=p[i];
    }
}
int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {  int ans=-1;
        for(i=0; i<n; i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        scanner();
        for(i=0; i<=top; i++)
            for(j=0;j<=top;j++)
        {
            ans=max(ans,dis1(s[i],s[j]));
        }
        printf("%d\n",ans);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

[POJ2187]Beauty Contest(凸包+旋转卡壳)

题目描述传送门题解凸包+旋转卡壳裸题代码#include #include #include #include #include using namespace std; #define N 1000...

poj 2187 Beauty Contest 经典题目:凸包+旋转卡壳

题目:http://poj.org/problem?id=2187 /*经典题目:凸包加旋转卡壳 凸包第一题, */ #include #include #include #include #...

POJ 2187 Beauty Contest(凸包)

Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest,...

POJ 2187 Beauty Contest(凸包:最远点对距离)

POJ 2187 Beauty Contest(凸包:最远点对距离) http://poj.org/problem?id=2187 题意:        平面上给你n个点,要你求出这n个点中的任...

poj2187——Beauty Contest(凸包+旋转卡壳)

DescriptionBessie, Farmer John’s prize cow, has just won first place in a bovine beauty contest, ear...

poj 2187 Beauty Contest 凸包最长直径

题意:求所有点中最远距离,即凸包的最长直径                  思路:求凸包,旋转卡壳求最长直径,枚举各个点,找对踵点,更新距离                  代码:     ...

POJ 2187 Beauty Contest(凸包 + 旋转卡壳)

Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: ...

poj2187 Beauty Contest 平面最远点对(凸包)

题意:给你n个点,问距离最远的两个点的距离的平方是多少。思路:最远点对一定在凸包上,所以枚举凸包上的点即可。代码:#include #include #include #include #def...

POJ 2187 Beauty Contest(凸包求最远点距离)

Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30924   Accepted: ...

【计算几何】 Andrew凸包算法 + 旋转卡壳(以求点对最长距离为例) -- 以 POJ 2187 Beauty Contest 为例

Part One 求凸包 凸包的算法有很多种,我这里使用的是是Andrew那个凸包算法,就是先循环所有点求出下凸包,再逆向循环所有点定求出上凸包的那个算法。 时间复杂度为O(NlogN) 一个j...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 2187 Beauty Contest(凸包)
举报原因:
原因补充:

(最多只允许输入30个字)