• poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方


    旋转卡壳求凸包的直径的平方

    板子题


    #include<cstdio>
    #include<vector>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    struct Point {
        int x, y;
        Point(int x=0, int y=0):x(x),y(y) { }
    };
    
    typedef Point Vector;
    
    Vector operator - (const Point& A, const Point& B) {
        return Vector(A.x-B.x, A.y-B.y);
    }
    
    int Cross(const Vector& A, const Vector& B) {
        return A.x*B.y - A.y*B.x;
    }
    
    int Dot(const Vector& A, const Vector& B) {
        return A.x*B.x + A.y*B.y;
    }
    
    int Dist2(const Point& A, const Point& B) {
        return (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y);
    }
    
    bool operator < (const Point& p1, const Point& p2) {
        return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);
    }
    
    bool operator == (const Point& p1, const Point& p2) {
        return p1.x == p2.x && p1.y == p2.y;
    }
    
    // 点集凸包
    // 假设不希望在凸包的边上有输入点,把两个 <= 改成 <
    // 注意:输入点集会被改动
    vector<Point> ConvexHull(vector<Point>& p) {
        // 预处理。删除反复点
        sort(p.begin(), p.end());
        p.erase(unique(p.begin(), p.end()), p.end());
    
        int n = p.size();
        int m = 0;
        vector<Point> ch(n+1);
        for(int i = 0; i < n; i++) {
            while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
            ch[m++] = p[i];
        }
        int k = m;
        for(int i = n-2; i >= 0; i--) {
            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--;
        ch.resize(m);
        return ch;
    }
    
    //返回点集直径的平方
    int diameter2(vector<Point> & points) {
        vector<Point> p = ConvexHull(points);
        int n = p.size();
        if(n==1) return 0;
        if(n==2) return Dist2(p[0], p[1]);
        p.push_back(p[0]);
        int ans = 0;
        for(int u = 0, v = 1; u < n; ++u) {
            for(;;) {
                int diff = Cross(p[u+1]-p[u], p[v+1]-p[v]);
                if(diff<=0) {
                    ans = max(ans, Dist2(p[u], p[v]));
                    if(diff==0) ans = max(ans, Dist2(p[u], p[v+1]));
                    break;
                }
                v = (v+1) % n;
            }
        }
        return ans;
    }
    int main() {
        int n;
        scanf("%d", &n);
        vector<Point> P;
        for(int i=0; i<n; ++i) {
            int x, y;
            scanf("%d%d", &x, &y);
            P.push_back(Point(x, y));
        }
        printf("%d
    ", diameter2(P));
        return 0;
    }
    
    


  • 相关阅读:
    mysql启动失败
    mini.open参数传递
    json的key动态赋值
    Python文件格式 .py .pyc .pyw .pyo .pyd的主要区别
    centos 7.2 安装mongodb 3.4.4免编译
    tomcat 8.0安装ssl证书,及centos7.2 的openssl升级到最新版本,及ERR_SSL_OBSOLETE_CIPHER错误解决
    云服务器 Centos7.0 部署
    笔记
    oracle 视图的创建,游标,left join
    在Extjs中对日期的处理,以及在后端数据在SQL语句的判断处理
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6705450.html
Copyright © 2020-2023  润新知