• 原题如下:

    Beauty Contest
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 42961   Accepted: 13307

    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) 
  • 题解:
    • 显然,如果某个点在另外三个点组成的三角形的内部,那么它就不可能属于最远点对,因而可以删去。这样,最后需要考虑的点,就只剩下不在任意三个点组成的三角形内部的,所给点集中最外围的点了。这些最外围的点的集合,就是包围原点集的最小凸多边形的顶点组成的集合,称为原点集的凸包。因为顶点的坐标限定为整数,坐标值的范围不超过M的凸多边形的顶点数只有O((√M)2/3)个,所以只要枚举凸包上的所有点对并计算距离就可以求得最远点对了。
    • 求凸包的算法很多,求n个点集对应的凸包,只要O(nlogn)时间。
      一种基于平面扫描法的Graham扫描算法:
      首先,把点集按x坐标→y坐标的字典序升序排序,那么排序后的第一个和最后一个点必然是凸包上的顶点,它们之间的部分可以分成上下两条链分别求解。求下侧的链时只要从小到大处理排序后的点列,逐步构造凸包,在构造过程中的凸包末尾加上新的顶点后,可能会破坏凸性,此时只要将凹的部分的点从末尾出去就好了。求上侧的链也是一样地从大到小处理即可。排序的复杂度为O(nlogn),剩余部分处理的复杂度为O(n)。
    • 旋转卡壳法:
      假设最远点对是p和q,那么p就是点集中(p-q)方向最远的点,而q是点集中(q-p)方向最远的点,因此,可以按照逆时针逐渐改变方向,同时枚举出所有对于某个方向上最远的点对(这样的点对又叫做对踵点对),那么最远点对一定也包含于其中。在逐渐改变方向的过程中,对踵点对只有在方向等于凸包某条边的法线方向时发生变化,此时点将向凸包上对应的相邻点移动。另方向逆时针旋转一周,那么对踵点对也在凸包上转了一周,这样就可以在凸包顶点的线性时间内求得最远点对。
      附一张示意图:
  • 代码1:
     #include <cstdio>
    #include <vector>
    #include <algorithm>
    #include <cmath> using namespace std; const double EPS=1e-; double add(double a, double b)
    {
    if (fabs(a+b)<EPS*(fabs(a)+fabs(b))) return ;
    return a+b;
    } struct P
    {
    double x, y;
    P(){}
    P(double x, double y): x(x), y(y) {}
    P operator - (P p)
    {
    return P(add(x, -p.x), add(y, -p.y));
    }
    double det(P p)
    {
    return add(x*p.y, -y*p.x);
    }
    double dot(P p)
    {
    return add(x*p.x, y*p.y);
    }
    }; const int MAX_N=;
    int N;
    P ps[MAX_N]; bool cmp_x(const P &p, P &q)
    {
    if (p.x!=q.x) return p.x<q.x;
    return p.y<q.y;
    } vector<P> convex_hull(P * ps, int n)
    {
    sort(ps, ps+n, cmp_x);
    int k=;
    vector<P> qs(n*);
    for (int i=; i<n; i++)
    {
    while (k> && (qs[k-]-qs[k-]).det(ps[i]-qs[k-])<=) k--;
    qs[k++]=ps[i];
    }
    for (int i=n-, t=k; i>=; i--)
    {
    while (k>t && (qs[k-]-qs[k-]).det(ps[i]-qs[k-])<=) k--;
    qs[k++]=ps[i];
    }
    qs.resize(k-);
    return qs;
    } double max(int x, int y)
    {
    if (x>y) return x;
    return y;
    } double dist(P p, P q)
    {
    return (p-q).dot(p-q);
    } int main()
    {
    scanf("%d", &N);
    for (int i=; i<N; i++)
    {
    scanf("%lf %lf", &ps[i].x, &ps[i].y);
    }
    vector<P> qs=convex_hull(ps, N);
    double res=;
    for (int i=; i<qs.size(); i++)
    {
    for (int j=; j<i; j++)
    {
    res=max(res, dist(qs[i], qs[j]));
    }
    }
    printf("%.0f\n", res);
    }

    代码2:

     #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <vector> using namespace std; const double EPS=1e-; double max(double x, double y)
    {
    if (x>y+EPS) return x;
    return y;
    } double add(double x, double y)
    {
    if (fabs(x+y)<EPS*(fabs(x)+fabs(y))) return ;
    return x+y;
    }
    struct P
    {
    double x, y;
    P(){}
    P(double x, double 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));
    }
    double dot(P p)
    {
    return add(x*p.x, y*p.y);
    }
    double det(P p)
    {
    return add(x*p.y, -y*p.x);
    }
    }; const int MAX_N=;
    int N;
    P ps[MAX_N]; bool cmp_x(const P &p, const P &q)
    {
    if (p.x!=q.x) return p.x<q.x;
    return p.y<q.y;
    } double dist(P p, P q)
    {
    return (p-q).dot(p-q);
    } vector<P> convex_hull(P * ps, int n)
    {
    sort(ps, ps+n, cmp_x);
    int k=;
    vector<P> qs(n*);
    for (int i=; i<n; i++)
    {
    while (k> && (qs[k-]-qs[k-]).det(ps[i]-qs[k-])<=) k--;
    qs[k++]=ps[i];
    }
    for (int i=n-, t=k; i>=; i--)
    {
    while (k>t && (qs[k-]-qs[k-]).det(ps[i]-qs[k-])<=) k--;
    qs[k++]=ps[i];
    }
    qs.resize(k-);
    return qs;
    } int main()
    {
    scanf("%d", &N);
    for (int i=; i<N; i++)
    {
    scanf("%lf %lf", &ps[i].x, &ps[i].y);
    }
    vector<P> qs=convex_hull(ps, N);
    int n=qs.size();
    if (n==)
    {
    printf("%.0f\n", dist(qs[], qs[]));
    return ;
    }
    int i=, j=;
    for (int k=; k<n; k++)
    {
    if (!cmp_x(qs[i], qs[k])) i=k;
    if (cmp_x(qs[j], qs[k])) j=k;
    }
    double res=;
    int si=i, sj=j;
    while (i!=sj || j!=si)
    {
    res=max(res, dist(qs[i], qs[j]));
    if ((qs[(i+)%n]-qs[i]).det(qs[(j+)%n]-qs[j])<)
    {
    i=(i+)%n;
    }
    else
    {
    j=(j+)%n;
    }
    }
    printf("%.0f\n", res);
    }

Beauty Contest(POJ 2187)的更多相关文章

  1. POJ-2187 Beauty Contest,旋转卡壳求解平面最远点对!

     凸包(旋转卡壳) 大概理解了凸包A了两道模板题之后在去吃饭的路上想了想什么叫旋转卡壳呢?回来无聊就搜了一下,结果发现其范围真广. 凸包: 凸包就是给定平面图上的一些点集(二维图包),然后求点集组成的 ...

  2. poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)

    /* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...

  3. 【POJ】2187 Beauty Contest(旋转卡壳)

    http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...

  4. poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)

    链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...

  5. POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]

    题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...

  6. 【POJ】【2187】Beauty Contest

    计算几何/旋转卡壳 学习旋转卡壳请戳这里~感觉讲的最好的就是这个了…… 其实就是找面积最大的三角形?...并且满足单调…… 嗯反正就是这样…… 这是一道模板题 好像必须写成循环访问?我在原数组后面复制 ...

  7. POJ 2187 Beauty Contest【旋转卡壳求凸包直径】

    链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  8. POJ 2187: Beauty Contest(旋转卡)

    id=2187">Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27218   ...

  9. poj 2187 Beauty Contest

    Beauty Contest 题意:给你一个数据范围在2~5e4范围内的横纵坐标在-1e4~1e4的点,问你任意两点之间的距离的最大值的平方等于多少? 一道卡壳凸包的模板题,也是第一次写计算几何的题, ...

随机推荐

  1. Jdk1.8下的HashMap源码分析

    目录结构 一.面试常见问题 二.基本常量属性 三.构造方法 四.节点结构        4.1 Node类        4.2.TreeNode 五.put方法        5.1 key的has ...

  2. [Hadoop] mapper数量的控制

    确定map任务数时依次优先参考如下几个原则: 1)      每个map任务使用的内存不超过800M,尽量在500M以下 比如处理256MB数据需要的时间为10分钟,内存为800MB,此时如果处理12 ...

  3. 复习 Array,重学 JavaScript

    1 数组与对象 在 JavaScript 中,一个对象的键只能有两种类型:string 和 symbol.下文只考虑键为字符串的情况. 1.1 创建对象 在创建对象时,若对象的键为数字,或者由 字母+ ...

  4. 深度优先搜索(dfs)与出题感想

    在3月23号的广度优先搜索(bfs)博客里,我有提到写一篇深搜博客,今天来把这个坑填上. 第一部分:深度优先搜索(dfs) 以上来自百度百科. 简单来说,深度优先搜索算法就是——穷举法,即枚举所有情况 ...

  5. 使用CrashHandler获取应用crash信息

      Android应用不可避免会发生crash,也称之为崩溃.发生原因可能是由于Android系统底层的bug,也可能是由于不充分的机型适配或者是糟糕的网络情况.当crash发生时,系统会kill掉正 ...

  6. 实现图像的二值化(java+opencv)

    书里的解释: 其他的没找到什么资料,直接参考百度百科 https://baike.baidu.com/item/%E5%9B%BE%E5%83%8F%E4%BA%8C%E5%80%BC%E5%8C%9 ...

  7. three.js 制作逻辑转体游戏(下)

    上一篇已经对绕非定轴转动有所了解,这篇郭先生继续说一说逻辑转体游戏的制作,这部分我们同样会遇到一些小问题,首先是根据数据渲染陷阱和目标区域,然后是对可以转动的判定,最后是获胜的判定. 1. 根据数据渲 ...

  8. 第2章 执行SparkSQL查询

    第2章 执行SparkSQL查询 2.1 命令行查询流程 打开Spark shell 例子:查询大于30岁的用户 创建如下JSON文件,注意JSON的格式: {"name":&qu ...

  9. python 基础-文件读写'r' 和 'rb'区别

    原文链接: python基础-文件读写'r' 和 'rb'区别 一.Python文件读写的几种模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r ...

  10. 2020.5.23 第三篇 Scrum冲刺博客

    Team:银河超级无敌舰队 Project:招新通 项目冲刺集合贴:链接 目录 一.每日站立会议 二.项目燃尽图 三.签入记录 3.1 代码/文档签入记录 3.2 主要代码截图 3.3 程序运行截图 ...