zoukankan      html  css  js  c++  java
  • POJ 2187 Beauty Contest凸包 旋转卡壳法

    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最大有50000个,直接枚举是不行的,这时可以建立一个凸包,在凸包里面的肯定不是最远两个端点中的一个,相距最远的一定在凸包里,只要枚举凸包里的点就行了,这样把很多不必须的计算都省去了,节约了大量的时间。

    在逐步建立凸包的时候,每加一个点可以判断下是否与前面的两点构成凹型,是的话把前一个点删掉就行了。当然这只是普通方法。

    #include <iostream>
    #include <stdio.h>
    #include <cmath>
    #include <string.h>
    #include <vector>
    #include <algorithm>
    using namespace std;
    const int N = 5e4+10;
    double EPS = 1e-10;
    double add(double a,double b) {
        if(abs(a+b) < EPS * (abs(a) + abs(b))) return 0;
        return a + b;
    }
    struct Point{
        double x, y;
        Point(){}
        Point(double x, double y) :x(x), y(y) {}
        Point operator + (Point p) {
            return Point(add(x, p.x), add(y, p.y));
        }
        Point operator - (Point p) {
            return Point(add(x, -p.x), add(y, -p.y));
        }
        Point operator * (double d) {
            return Point(x*d, y*d);
        }
        double dot(Point p) {    //内积
            return add(x * p.x, y * p.y);
        }
        double det(Point p) {    //外积
            return add(x * p.y, -y * p.x);
        }
    };
    Point ps[N];
    int n;
    bool cmp(Point x, Point y) {
        if(x.x != y.x) return x.x < y.x;
        else return x.y < y.y;
    }
    vector<Point> convex_hull(Point *ps, int n) {
        sort(ps, ps+n, cmp);
        int k = 0;
        vector<Point> qs(n*2);
        for(int i = 0; i < n; i ++) {   //下侧
            while(k > 1 && (qs[k-1] - qs[k-2]).det(ps[i] - qs[k-1]) <= 0) k--;
            qs[k++] = ps[i];
        }
        for(int i = n-2, t = k; i >= 0; i --) { //上侧
            while(k > t && (qs[k-1] - qs[k-2]).det(ps[i] - qs[k-1]) <= 0) k--;
            qs[k++] = ps[i];
        }
        qs.resize(k-1);
        return qs;
    }
    double dist(Point p1, Point p2) {
        return (p1-p2).dot(p1-p2);
    }
    void solve() {
        vector<Point> qs = convex_hull(ps, n);
        double res = 0;
        for(int i = 0; i < qs.size(); i ++) {
            for(int j = 0; j < i; j ++) {
                res = max(res, dist(qs[i], qs[j]));
            }
        }
        printf("%.0f
    ",res);
    }
    int main() {
        Point p1, p2, p3;
        p1.x = 1, p2.y = 4; p2.x = 2, p2.y = 1; p3.x = 4, p3.y = 1;
        cout << (p3-p2).det(p2-p1) << endl;
        scanf("%d", &n);
        for(int i = 0; i < n; i ++) {
            scanf("%lf %lf", &ps[i].x, &ps[i].y);
        }
        solve();
        return 0;
    }

    但凸包的点也有很大时,上面的方法就不好用了,这时可以用旋转卡(qiǎ)壳法,能在线性时间内算出来。

    假设最远点对是p和q,那么p就是点集中(p-q)方向最远的点,而q是点集中(q-p)方向最远的点,因此可以按照逆时针改变方向,同时枚举出所有对于某个方向上最远的点,那么最远点一定也包含在其中。下面这张图就更好的说明了。

    旋转卡壳法

    (图片来源http://cgm.cs.mcgill.ca/

    下面是代码:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cmath>
     4 #include <string.h>
     5 #include <vector>
     6 #include <algorithm>
     7 using namespace std;
     8 const int N = 5e4+10;
     9 double EPS = 1e-10;
    10 double add(double a,double b) {
    11     if(abs(a+b) < EPS * (abs(a) + abs(b))) return 0;
    12     return a + b;
    13 }
    14 struct Point{
    15     double x, y;
    16     Point(){}
    17     Point(double x, double y) :x(x), y(y) {}
    18     Point operator + (Point p) {
    19         return Point(add(x, p.x), add(y, p.y));
    20     }
    21     Point operator - (Point p) {
    22         return Point(add(x, -p.x), add(y, -p.y));
    23     }
    24     Point operator * (double d) {
    25         return Point(x*d, y*d);
    26     }
    27     double dot(Point p) {    //内积
    28         return add(x * p.x, y * p.y);
    29     }
    30     double det(Point p) {    //外积
    31         return add(x * p.y, -y * p.x);
    32     }
    33 };
    34 Point ps[N];
    35 int n;
    36 bool cmp(Point x, Point y) {
    37     if(x.x != y.x) return x.x < y.x;
    38     else return x.y < y.y;
    39 }
    40 vector<Point> convex_hull(Point *ps, int n) {
    41     sort(ps, ps+n, cmp);
    42     int k = 0;
    43     vector<Point> qs(n*2);
    44     for(int i = 0; i < n; i ++) {   //下侧
    45         while(k > 1 && (qs[k-1] - qs[k-2]).det(ps[i] - qs[k-1]) <= 0) k--;
    46         qs[k++] = ps[i];
    47     }
    48     for(int i = n-2, t = k; i >= 0; i --) { //上侧
    49         while(k > t && (qs[k-1] - qs[k-2]).det(ps[i] - qs[k-1]) <= 0) k--;
    50         qs[k++] = ps[i];
    51     }
    52     qs.resize(k-1);
    53     return qs;
    54 }
    55 double dist(Point p1, Point p2) {
    56     return (p1-p2).dot(p1-p2);
    57 }
    58 void solve() {
    59     vector<Point> qs = convex_hull(ps, n);
    60     int n = qs.size();
    61     if(n == 2) {
    62         printf("%.0f
    ",dist(qs[0], qs[1]));
    63         return;
    64     }
    65     int i = 0, j = 0;
    66     for(int k = 0; k < n; k ++) {
    67         if(!cmp(qs[i], qs[k])) i = k;
    68         if(cmp(qs[j], qs[k])) j = k;
    69     }
    70     double res = 0;
    71     int si = i, sj = j;
    72     while(si != j || sj != i) {
    73         res = max(res, dist(qs[i], qs[j]));
    74         if((qs[(i+1)%n] - qs[i]).det(qs[(j+1)%n] - qs[j]) < 0) {
    75             i = (i + 1) % n;
    76         } else j = (j + 1) % n;
    77     }
    78     printf("%.0f
    ",res);
    79 }
    80 int main() {
    81     Point p1, p2, p3;
    82     p1.x = 1, p2.y = 4; p2.x = 2, p2.y = 1; p3.x = 4, p3.y = 1;
    83     cout << (p3-p2).det(p2-p1) << endl;
    84     scanf("%d", &n);
    85     for(int i = 0; i < n; i ++) {
    86         scanf("%lf %lf", &ps[i].x, &ps[i].y);
    87     }
    88     solve();
    89     return 0;
    90 }
  • 相关阅读:
    ansible的使用
    Selenium2+python自动化68-html报告乱码问题【转载】
    Selenium2+python自动化67-用例失败自动截图【转载】
    Selenium2+python自动化66-装饰器之运行失败截图【转载】
    Selenium2+python自动化65-js定位几种方法总结【转载】
    selenium+python自动化82-只截某个元素的图【转载】
    selenium+python自动化81-html报告优化(饼图+失败重跑+兼容python2&3)【转载】
    selenium+python自动化79-文件下载(SendKeys)【转载】
    selenium+python自动化77-autoit文件上传【转载】
    Pycharm上python和unittest两种姿势傻傻分不清楚【转载】
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7300231.html
Copyright © 2011-2022 走看看