• POJ 2187 Beauty Contest (凸包 旋转卡壳)


    题目链接:POJ 2187

    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)

    Solution

    题意

    给定 (n) 个点的坐标,求这些点构成的凸包的直径的平方。

    题解

    凸包 旋转卡壳

    旋转卡壳求凸包直径的模板题。

    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <vector>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const double eps = 1e-8;
    const int inf = 0x3f3f3f3f;
    const int maxn = 100000 + 5;
    
    int n;
    
    class Point {
    public:
        double x, y;
        Point(double x = 0, double y = 0) : x(x), y(y) {}
        Point operator+(Point a) {
            return Point(a.x + x, a.y + y);
        }
        Point operator-(Point a) {
            return Point(x - a.x, y - a.y);
        }
        bool operator<(const Point &a) const {
            if (x == a.x)
                return y < a.y;
            return x < a.x;
        }
        bool operator==(const Point &a) const {
            if (x == a.x && y == a.y)
                return 1;
            return 0;
        }
    };
    
    typedef Point Vector;
    
    // 叉积
    double cross(Vector a, Vector b) {
        return a.x * b.y - a.y * b.x;
    }
    
    // 点积
    double dot(Vector a, Vector b) {
        return a.x * b.x + a.y * b.y;
    }
    
    // 判断 p2 是否在向量 p0p1 的顺时针方向
    bool isclock(Point p0, Point p1, Point p2) {
        Vector a = p1 - p0;
        Vector b = p2 - p0;
        if (cross(a, b) < -eps)
            return true;
        return false;
    }
    
    // 点a 点b的距离
    double getDistance(Point a, Point b) {
        return (pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
    }
    
    typedef vector<Point> Polygon;
    // 凸包
    Polygon Andrew(Polygon s) {
        Polygon u, l;
        if(s.size() < 3) return s;
        sort(s.begin(), s.end());
        u.push_back(s[0]);
        u.push_back(s[1]);
        l.push_back(s[s.size() - 1]);
        l.push_back(s[s.size() - 2]);
        for(int i = 2 ; i < s.size() ; ++i) {
            for(int n = u.size() ; n >= 2 && !isclock(u[n - 2], u[n - 1], s[i]); --n) {
                u.pop_back();
            }
            u.push_back(s[i]);
        }
        for(int i = s.size() - 3 ; i >= 0 ; --i) {
            for(int n = l.size() ; n >= 2 && !isclock(l[n - 2], l[n - 1], s[i]); --n) {
                l.pop_back();
            }
            l.push_back(s[i]);
        }
        for(int i = 1 ; i < u.size() - 1 ; i++) l.push_back(u[i]);
        return l;
    }
    
    // 点 x到直线 ab 的距离
    double dis(Point a, Point b, Point x) {
        return fabs(dot((a - x), (b - x)) / getDistance(a, b));
    }
    
    // 旋转卡壳求凸包直径
    double rotating_caliper(vector<Point> v) {
    	double max_dis = 0.0;
    	int n = v.size();
    	if (n == 2) {
    		return getDistance(v[0], v[1]);
    	}
    
        v.push_back(v[0]);
        int j = 2;
        for (int i = 0; i < n; ++i) {
            while (dis(v[i], v[i + 1], v[j]) < dis(v[i], v[i + 1], v[j + 1])) {
                j = (j + 1) % n;
            }
            max_dis = max(max_dis, max(getDistance(v[j], v[i]), getDistance(v[j], v[i + 1])));
        }
    	
    	return max_dis;
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> n;
        Polygon s;
        for(int i = 0; i < n; ++i) {
            Point p;
            cin >> p.x >> p.y;
            s.push_back(p);
        }
        Polygon p = Andrew(s);
        double d = rotating_caliper(p);
        cout << d << endl;
        return 0;
    }
    
  • 相关阅读:
    聊天类功能测试用例
    即时通讯软件针对通讯以及协议方面有哪些测试点?
    面试前期准备工作
    黑盒功能业务测试过程
    Web网站实现facebook登录
    Nginx配置SSL实现HTTPS访问
    jQuery判断当前页面是APP内打开还是浏览器打开
    jQuery实现点击图片简单放大效果
    Linux排查PHP-FPM进程过量常用命令
    PHP防止SQL注入攻击和XSS攻击
  • 原文地址:https://www.cnblogs.com/wulitaotao/p/11384775.html
Copyright © 2020-2023  润新知