zoukankan      html  css  js  c++  java
  • UVa 11168 Airport , 凸包

    题意:

    给出平面上n个点,找一条直线,使得全部点在直线的同側。且到直线的距离之平均值尽量小。 


    先求凸包

    易知最优直线一定是凸包的某条边,然后利用点到直线距离公式进行计算。



    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<cmath>
    #include<algorithm>
    #include<iostream>
    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 Vector& a, const Vector& b) {
        return Vector(a.x+b.x, a.y+b.y);
    }
    Vector operator - (const Vector& a, const Vector& b) {
        return Vector(a.x-b.x, a.y-b.y);
    }
    Vector operator * (const Vector& a, double p) {
        return Vector(a.x*p, a.y*p);
    }
    Vector operator / (const Vector& a, double p) {
        return Vector(a.x/p, a.y/p);
    }
    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;
    }
    
    int Cross(const Vector& a, const Vector& b) {
        return a.x*b.y - a.y*b.x;
    }
    
    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;
    }
    
    // 过两点p1, p2的直线一般方程ax+by+c=0
    // (x2-x1)(y-y1) = (y2-y1)(x-x1)
    void getLineGeneralEquation(const Point& p1, const Point& p2, double& a, double& b, double &c) {
      a = p2.y-p1.y;
      b = p1.x-p2.x;
      c = -a*p1.x - b*p1.y;
    }
    
    int main()
    {
        int t, n, i, j;
        scanf("%d", &t);
        for(int cas=1; cas<=t; ++cas)
        {
            scanf("%d", &n);
            int x, y;
            vector<Point> P;
            double sumx = 0, sumy = 0;
            for(i=0; i<n; ++i)
            {
                scanf("%d%d", &x, &y);
                sumx += x;
                sumy += y;
                P.push_back(Point(x,y));
            }
            P = ConvexHull(P);
            int m = P.size();
            double ans = 1e9;
            if(m<=2) ans = 0;
            else
            for(i=0; i<m; ++i)
            {
                    j = (i+1)%m;
                    double A, B, C;
                    getLineGeneralEquation(P[i], P[j], A, B, C);
                    double tmp = fabs(A*sumx + B*sumy + C*n) / sqrt(A*A+B*B);
                    ans = min(ans, tmp);
            }
            printf("Case #%d: %.3f
    ", cas, ans/n);
        }
        return 0;
    }
    
    
    
    
    
    
    
    
    
    


  • 相关阅读:
    SEO在网页制作中的应用
    日期控件选2016-01-01却变为2015-01-01问题
    IE8兼容placeholder的方案
    各种乱码,编码问题设置方法整理(UTF-8)
    JQuery简单实现图片轮播效果
    解决 jsp eclipse异常 【The import javax.servlet cannot be resolved】
    tomcat加载不了spring-webjar终极解决办法
    MyEclipse Web Project导入Eclipse Dynamic Web Project,无法部署到tomcat问 题
    2020-11-01助教一周总结(第九周)
    2020-10-25助教一周总结(第八周)
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7095590.html
Copyright © 2011-2022 走看看