Beauty Contest(POJ 2187)

  • 原题如下:
    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:
     1 #include <cstdio>
     2 #include <vector>
     3 #include <algorithm>
     4 #include <cmath>
     5 
     6 using namespace std;
     7 
     8 const double EPS=1e-10;
     9 
    10 double add(double a, double b)
    11 {
    12     if (fabs(a+b)<EPS*(fabs(a)+fabs(b))) return 0;
    13     return a+b;
    14 }
    15 
    16 struct P
    17 {
    18     double x, y;
    19     P(){}
    20     P(double x, double y): x(x), y(y) {} 
    21     P operator - (P p)
    22     {
    23         return P(add(x, -p.x), add(y, -p.y));
    24     }
    25     double det(P p)
    26     {
    27         return add(x*p.y, -y*p.x); 
    28     }
    29     double dot(P p)
    30     {
    31         return add(x*p.x, y*p.y);
    32     }
    33 };
    34 
    35 const int MAX_N=50005;
    36 int N;
    37 P ps[MAX_N];
    38 
    39 bool cmp_x(const P &p, P &q)
    40 {
    41     if (p.x!=q.x) return p.x<q.x;
    42     return p.y<q.y;
    43 }
    44 
    45 vector<P> convex_hull(P * ps, int n)
    46 {
    47     sort(ps, ps+n, cmp_x);
    48     int k=0;
    49     vector<P> qs(n*2);
    50     for (int i=0; i<n; i++)
    51     {
    52         while (k>1 && (qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
    53         qs[k++]=ps[i];
    54     }
    55     for (int i=n-2, t=k; i>=0; i--)
    56     {
    57         while (k>t && (qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
    58         qs[k++]=ps[i];
    59     }
    60     qs.resize(k-1);
    61     return qs;
    62 }
    63 
    64 double max(int x, int y)
    65 {
    66     if (x>y) return x;
    67     return y;
    68 }
    69 
    70 double dist(P p, P q)
    71 {
    72     return (p-q).dot(p-q);
    73 }
    74 
    75 int main()
    76 {
    77     scanf("%d", &N);
    78     for (int i=0; i<N; i++)
    79     {
    80         scanf("%lf %lf", &ps[i].x, &ps[i].y);
    81     }
    82     vector<P> qs=convex_hull(ps, N);
    83     double res=0;
    84     for (int i=0; i<qs.size(); i++)
    85     {
    86         for (int j=0; j<i; j++)
    87         {
    88             res=max(res, dist(qs[i], qs[j]));
    89         }
    90     }
    91     printf("%.0f
    ", res);
    92 }

     代码2:

      1 #include <cstdio>
      2 #include <cmath>
      3 #include <algorithm>
      4 #include <vector>
      5 
      6 using namespace std;
      7 
      8 const double EPS=1e-10;
      9 
     10 double max(double x, double y)
     11 {
     12     if (x>y+EPS) return x;
     13     return y;
     14 }
     15 
     16 double add(double x, double y)
     17 {
     18     if (fabs(x+y)<EPS*(fabs(x)+fabs(y))) return 0;
     19     return x+y;
     20 }
     21 struct P
     22 {
     23     double x, y;
     24     P(){}
     25     P(double x, double y): x(x), y(y) {}
     26     P operator + (P p)
     27     {
     28         return P(add(x, p.x), add(y, p.y));
     29     }
     30     P operator - (P p)
     31     {
     32         return P(add(x, -p.x), add(y, -p.y));
     33     }
     34     double dot(P p)
     35     {
     36         return add(x*p.x, y*p.y);
     37     }
     38     double det(P p)
     39     {
     40         return add(x*p.y, -y*p.x);
     41     }
     42 };
     43 
     44 const int MAX_N=50005;
     45 int N;
     46 P ps[MAX_N];
     47 
     48 bool cmp_x(const P &p, const P &q)
     49 {
     50     if (p.x!=q.x) return p.x<q.x;
     51     return p.y<q.y;
     52 }
     53 
     54 double dist(P p, P q)
     55 {
     56     return (p-q).dot(p-q);
     57 }
     58 
     59 vector<P> convex_hull(P * ps, int n)
     60 {
     61     sort(ps, ps+n, cmp_x);
     62     int k=0;
     63     vector<P> qs(n*2);
     64     for (int i=0; i<n; i++)
     65     {
     66         while (k>1 && (qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
     67         qs[k++]=ps[i];
     68     }
     69     for (int i=n-2, t=k; i>=0; i--)
     70     {
     71         while (k>t && (qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
     72         qs[k++]=ps[i];
     73     }
     74     qs.resize(k-1);
     75     return qs;
     76 }
     77 
     78 int main()
     79 {
     80     scanf("%d", &N);
     81     for (int i=0; i<N; i++)
     82     {
     83         scanf("%lf %lf", &ps[i].x, &ps[i].y);
     84     }    
     85     vector<P> qs=convex_hull(ps, N);
     86     int n=qs.size();
     87     if (n==2)
     88     {
     89         printf("%.0f
    ", dist(qs[0], qs[1]));
     90         return 0;
     91     }
     92     int i=0, j=0;
     93     for (int k=0; k<n; k++)
     94     {
     95         if (!cmp_x(qs[i], qs[k])) i=k;
     96         if (cmp_x(qs[j], qs[k])) j=k;
     97     }
     98     double res=0;
     99     int si=i, sj=j;
    100     while (i!=sj || j!=si)
    101     {
    102         res=max(res, dist(qs[i], qs[j]));
    103         if ((qs[(i+1)%n]-qs[i]).det(qs[(j+1)%n]-qs[j])<0)
    104         {
    105             i=(i+1)%n;
    106         }
    107         else
    108         {
    109             j=(j+1)%n;
    110         }
    111     }
    112     printf("%.0f
    ", res);
    113 }
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9791514.html