zoukankan      html  css  js  c++  java
  • POJ 2187 Beauty Contest (凸包 旋转卡壳 平面最远点对 凸包直径)

    题目: 传送门

    题意: 给你 n 个点, 问你这些点中, 最远的两个点的距离的平方是多少。

     

    题解: 挑战 261 有细讲, 求凸包 + 旋转卡(qia)壳。

           旋转卡壳详解

    码一:

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <vector>
    #include <set>
    #include <string>
    #include <math.h>
    #define LL long long
    #define mem(i, j) memset(i, j, sizeof(i))
    #define rep(i, j, k) for(int i = j; i <= k; i++)
    #define dep(i, j, k) for(int i = k; i >= j; i--)
    #define pb push_back
    #define make make_pair
    #define INF INT_MAX
    #define inf LLONG_MAX
    #define PI acos(-1)
    using namespace std;
    
    const int N = 5e4 + 5;
    
    struct Point {
        double x, y;
        Point(double x = 0, double y = 0) : x(x), y(y) { } /// 构造函数
    };
    
    typedef Point Vector;
    /// 向量+向量=向量, 点+向量=向量
    Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
    ///点-点=向量
    Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
    ///向量*数=向量
    Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); }
    ///向量/数=向量
    Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }
    
    const double eps = 1e-8;
    int dcmp(double x) {
        if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
    }
    
    bool operator < (const Point& a, const Point& b) {
        return a.x == b.x ? a.y < b.y : a.x < b.x;
    }
    
    bool operator == (const Point& a, const Point &b) {
        return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
    }
    
    double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } /// 点积
    double Length(Vector A) { return sqrt(Dot(A, A)); } /// 计算向量长度
    double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
    double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积
    
    
    Point P[N], Q[N];
    
    int ConvexHull(Point* p, int n, Point* ch) {
        sort(p, p + n);
        int m = 0;
        rep(i, 0, n - 1) {
            while(m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
            ch[m++] = p[i];
        }
        int k = m;
        dep(i, 0, n - 2) {
            while(m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
            ch[m++] = p[i];
        }
        if(n > 1) m--;
        return m;
    }
    
    void GetMax(int n) {
        if(n == 2) {
            printf("%.0f
    ", Dot(Q[1] - Q[0], Q[1] - Q[0]));
            return ;
        }
    
        double res = 0;
    
        int cur = 1;
        rep(i, 0, n - 1) {
            while(dcmp(Cross(Q[(i + 1) % n] - Q[i], Q[cur] - Q[i]) - Cross(Q[(i + 1) % n] - Q[i], Q[(cur +1) % n] - Q[i])) < 0) cur = (cur + 1) % n;
            res = max(res, max(Dot(Q[cur] - Q[i], Q[cur] - Q[i]), Dot(Q[cur] - Q[(i + 1) % n], Q[cur] - Q[(i + 1) % n])));
        }
    
        printf("%.0f
    ", res);
    }
    
    int main() {
        int n;
        scanf("%d", &n);
        rep(i, 0, n - 1) scanf("%lf %lf", &P[i].x, &P[i].y);
        if(n == 2) {
            printf("%.0f
    ", Dot(P[0] - P[1], P[0] - P[1]));
        }
        else {
            n = ConvexHull(P, n, Q);
            GetMax(n);
        }
        return 0;
    }

    码二:

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <vector>
    #include <set>
    #include <string>
    #include <math.h>
    #define LL long long
    #define mem(i, j) memset(i, j, sizeof(i))
    #define rep(i, j, k) for(int i = j; i <= k; i++)
    #define dep(i, j, k) for(int i = k; i >= j; i--)
    #define pb push_back
    #define make make_pair
    #define INF INT_MAX
    #define inf LLONG_MAX
    #define PI acos(-1)
    using namespace std;
    
    const int N = 5e4 + 5;
    
    struct Point {
        double x, y;
        Point(double x = 0, double y = 0) : x(x), y(y) { } /// 构造函数
    };
    
    typedef Point Vector;
    /// 向量+向量=向量, 点+向量=向量
    Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
    ///点-点=向量
    Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
    ///向量*数=向量
    Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); }
    ///向量/数=向量
    Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }
    
    const double eps = 1e-10;
    int dcmp(double x) {
        if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
    }
    
    bool operator < (const Point& a, const Point& b) {
        return a.x == b.x ? a.y < b.y : a.x < b.x;
    }
    
    bool operator == (const Point& a, const Point &b) {
        return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
    }
    
    double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } /// 点积
    double Length(Vector A) { return sqrt(Dot(A, A)); } /// 计算向量长度
    double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
    double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积
    
    
    Point P[N], Q[N];
    
    int ConvexHull(Point* p, int n, Point* ch) {
        sort(p, p + n);
        int m = 0;
        rep(i, 0, n - 1) {
            while(m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
            ch[m++] = p[i];
        }
        int k = m;
        dep(i, 0, n - 2) {
            while(m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
            ch[m++] = p[i];
        }
        if(n > 1) m--;
        return m;
    }
    
    void GetMax(int n) {
        if(n == 2) {
            printf("%.0f
    ", Dot(Q[1] - Q[0], Q[1] - Q[0]));
            return ;
        }
        int i = 0, j = 0;
        rep(k, 0, n - 1) {
            if(!(Q[i] < Q[k])) i = k;
            if(Q[j] < Q[k]) j = k;
        }
        double res = 0;
        int si = i, sj = j;
        while(i != sj || j != si) {
            res = max(res, Dot(Q[i] - Q[j], Q[i] - Q[j]));
            if(Cross(Q[(i + 1) % n] - Q[i], Q[(j + 1) % n] - Q[j]) < 0) {
                i = (i + 1) % n;
            }
            else {
                j = (j + 1) % n;
            }
        }
        printf("%.0f
    ", res);
    }
    
    int main() {
        int n;
        scanf("%d", &n);
        rep(i, 0, n - 1) scanf("%lf %lf", &P[i].x, &P[i].y);
        if(n == 2) {
            printf("%.0f
    ", Dot(P[0] - P[1], P[0] - P[1]));
        }
        else {
            n = ConvexHull(P, n, Q);
            GetMax(n);
        }
        return 0;
    }
    一步一步,永不停息
  • 相关阅读:
    ColorPix——到目前为止最好用的屏幕取色器
    ES+VBA 实现批量添加网络图片
    SQL语句-delete语句
    Visual C++ 2013 and Visual C++ Redistributable Package 更新版官网下载地址
    centos长ping输出日志的脚本
    Centos 常用命令
    c#连接数据库
    C#窗体间的跳转传值
    c#邮件发送
    C#WIFI搜索与连接
  • 原文地址:https://www.cnblogs.com/Willems/p/12345090.html
Copyright © 2011-2022 走看看