关闭

POJ 2187 Beauty Contest(旋转卡壳模板题)

标签: 计算几何旋转卡壳
4040人阅读 评论(0) 收藏 举报
分类:

传送门
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 33435 Accepted: 10361
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

USACO 2003 Fall

题目大意:
就是求一个最远点对,给定平面上的一些散点集,求最远两点距离的平方值。

解题思路:
这是一个裸的旋转卡壳平面的最远点对或者是凸包的直径的题目,直接照着板子敲就行,直接上代码;

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const double INF = 1e18+5;
const int MAXN = 1e6 + 5;
const double eps = 1e-10;
const double PI = acos(-1.0);
int double_cmp(double x)
{
    if(fabs(x) < eps)
        return 0;
    if(x > 0)
        return 1;
    return -1;
}

struct Point
{
    double x, y;
    int id;
    Point() {}
    Point (double _x, double _y, int i):x(_x),y(_y),id(i) {}
    bool operator <(const struct Point &tmp)const
    {
        if(double_cmp(x-tmp.x) == 0)
            return double_cmp(y-tmp.y) < 0;
        return double_cmp(x-tmp.x) < 0;
    }
    bool operator == (const struct Point &tmp)const
    {
        return double_cmp(x-tmp.x)==0&&double_cmp(y-tmp.y)==0;
    }
} p[MAXN],st[MAXN];
double XMulti(Point a, Point b, Point c)///ac X ab
{
    return (c.x-a.x)*(b.y-a.y) - (b.x-a.x)*(c.y-a.y);
}

double dis(Point a, Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
double dis2(Point a, Point b)
{
    return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
}
double dot(Point a, Point b, Point c)///点积 ab . ac
{
    double s1 = b.x-a.x;
    double t1 = b.y-a.y;
    double s2 = c.x-a.x;
    double t2 = c.y-a.y;
    return s1*s2 + t1*t2;
}

int ConvexHull(Point *p, int n, Point *st)///凸包
{
    sort(p, p+n);
    n = unique(p, p+n)-p;///去重
    int m = 0;
    for(int i=0; i<n; i++)
    {
        while(m>1 && XMulti(st[m-2],p[i],st[m-1])<=0)
            m--;
        st[m++] = p[i];
    }
    int k = m;
    for(int i=n-2; i>=0; i--)
    {
        while(m>k && XMulti(st[m-2],p[i],st[m-1])<=0)
            m--;
        st[m++] = p[i];
    }
    if(n > 1)
        m--;
    return m;
}
double rotating_calipers(Point *p, int n)///旋转卡壳求凸包的直径,平面最远的点对
{
    int k=1;
    double ans = 0;
    p[n]=p[0];
    for(int i=0; i<n; i++)
    {
        while(fabs(XMulti(p[i+1],p[k],p[i]))<fabs(XMulti(p[i+1],p[k+1],p[i])))
            k=(k+1)%n;
        ans = max(ans,max(dis2(p[i],p[k]),dis2(p[i+1],p[k])));///注意是用的函数dis2
    }
    return ans;
}
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0; i<n; i++)
        {
            cin>>p[i].x>>p[i].y;
        }
        int m = ConvexHull(p, n, st);
        double ans = rotating_calipers(st, m);
        printf("%0.0lf\n",ans);
    }
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:487175次
    • 积分:11121
    • 等级:
    • 排名:第1605名
    • 原创:637篇
    • 转载:5篇
    • 译文:0篇
    • 评论:93条
    博客专栏