zoukankan      html  css  js  c++  java
  • poj2187 Beauty Contest (凸包 + 旋转卡壳)

    Beauty Contest

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 38349   Accepted: 11851

    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) 

    题意:

    给定 n 个点,求其中最远的两个点的距离的平方。

    思路:

    最远的两个点必定在凸包上,先求凸包,再用旋转卡壳求解。

    总结:

    旋转卡壳:现在凸包上找到一对点 Pi, Pj,Pj 是凸包上距 Pi 最远的点,则距离 Pi+1 (设Pi+1 在 Pi 的顺时针方向上的下一个点)最远的点必定在 Pj 的顺时针方向上(含 Pj);

    在判断是否是最远距离上,通过两个相邻的点设为( Pi, Pi+1 ),在凸包上按顺(逆)时针方向遍历,查找一个 Pj+1 的距离小于 Pj 的距离,则 Pj 就是 Pi 的最远点、 Pj+1 是 Pi+1 的最远点。距离通过向量的叉乘,即 Pi, Pi+1, Pj(Pj+1)所围成的三角形面积判断。三角形底边长不变,面积越大则高越长,即顶点距点边两端点距离越远。

    又求凸包时已经将凸包上的点按顺(逆)时针排列,据此,可在 O(n) 的时间内计算出凸包上所有点及与其相距最远的点。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #define N 100005
    #define eps 1e-8
    
    using namespace std;
    
    struct point
    {
        double x, y;
        point(){}
        point(double a, double b):x(a), y(b){}
        point operator-(point a){//向量减法
            return point(x-a.x, y-a.y);
        }
        point operator+(point a){//向量加法
            return point(x+a.x, y+a.y);
        }
        double operator*(point a){//向量叉积
            return x*a.y-y*a.x;
        }
        bool operator<(const point a)const{
            if(fabs(x-a.x)<eps)return y<a.y;//浮点数的判等不能直接用‘==’直接比较
            return x<a.x;
        }
        bool operator==(const point a)const{
            return (fabs(x-a.x)==eps && fabs(y-a.y));
        }
        double len(){//向量的模
            return sqrt(x*x+y*y);
        }
        double len2(){//向量的模的平方
            return (x*x+y*y);
        }
    }p[N], s[N];//p为点,s为栈
    
    double cp(point a, point b, point o)//向量oa,ob叉积
    {
        return (a-o)*(b-o);
    }
    
    void Convex(point *p, int &n)//Graham扫描法,栈内为所有凸包点
    {
        sort(p, p+n);
        int top, m;
        s[0] = p[0]; s[1] = p[1]; top = 1;
        for(int i = 2; i < n; i++)//从前往后扫
        {
            while(top>0 && cp(p[i], s[top], s[top-1])>=0)top--;
            s[++top] = p[i];
        }
        m = top;
        s[++top] = p[n-2];
        for(int i = n-3; i >= 0; i--)//从后往前扫
        {
            while(top>m && cp(p[i], s[top], s[top-1])>=0)top--;
            s[++top] = p[i];
        }
        n = top;
    }
    
    double rotating_calipers(point *ch,int n)//旋转卡壳
    {
        int q=1;
        double ans=0;
        ch[n]=ch[0];
        for(int p=0;p<n;p++)
        {
            while(((ch[p+1]-ch[p])*(ch[q+1]-ch[p])) > ((ch[p+1]-ch[p])*(ch[q]-ch[p])))
                q=(q+1)%n;
            ans=max(ans,max((ch[p]-ch[q]).len2(),(ch[p+1]-ch[q+1]).len2()));//此题要求最远距离的平方
        }
        return ans;
    }
    
    int main()
    {
        int n;
        while(scanf("%d", &n)!=EOF && n)
        {
            for(int i = 0; i < n; i++)
                  scanf("%lf%lf", &p[i].x, &p[i].y);
            sort(p, p+n);
            int cnt=unique(p, p+n) - p;
            Convex(p, cnt);
            int ans = rotating_calipers(s, cnt);
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    【转】Windows Phone的应用程序认证要求
    ObservableCollection删除问题
    国庆总结?
    .net dll破解实战
    理理头绪
    创建Metro风格的WPF界面
    Alpha项目测试
    原型设计
    团队项目总结
    最常用的35中心里效应
  • 原文地址:https://www.cnblogs.com/xiepingfu/p/7285190.html
Copyright © 2011-2022 走看看