zoukankan      html  css  js  c++  java
  • uva11168 凸包

    链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34780

    题意:给n个点,找一条直线,使得所有点在直线同侧,且到直线的平均距离最小。

    思路:先求出n个点围成的凸包,然后枚举凸包上的顶点,得到直线的一般方程,再计算出各点到直线的距离和。由于所有的点都在直线的同一侧,所以A*x0+B*y0+C的符号相同,故而可以求出所有点的x坐标和y坐标的和,总距离就很快算出来。注意凸包退化成一个点或者一条线段的情况,当退化成一个点时,A=B=0,除以sqrt(A*A+B*B)为无穷大,要特判一下,如果是一条线段,那结果就是0了。

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=10000+5;
    const double eps=1e-10;
    const int INF=0x3f3f3f3f;
    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 - (Vector A,Vector B)
    {
        return Vector(A.x-B.x,A.y-B.y);
    }
    double Cross(Vector A,Vector B)
    {
        return A.x*B.y-A.y*B.x;
    }
    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 dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;
    }
    struct Line//直线标准式
    {
        double a,b,c;
    };
    Line GetLine(Point A,Point B)//由两点式求直线标准方程
    {
        Line L;
        L.a=B.y-A.y;
        L.b=A.x-B.x;
        L.c=B.x*A.y-A.x*B.y;
        return L;
    }
    int cmp(const Point &a,const Point &b)
    {
        if(a.x<b.x) return 1;
        else if(a.x==b.x)
        {
            if(a.y<b.y) return 1;
            else return 0;
        }
        else return 0;
    }
    int ConvexHull(Point *p,int n,Point *ch)
    {
        sort(p,p+n,cmp);
        int m=0;
        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--;
        return m;
    }
    int main()
    {
     //   freopen("in.cpp","r",stdin);
        int t,ca=1;
        Point p[maxn],ch[maxn];
        scanf("%d",&t);
        while(t--)
        {
            int n;
            scanf("%d",&n);
            double x=0.0,y=0.0;
            for(int i=0; i<n; i++)
            {
                scanf("%lf%lf",&p[i].x,&p[i].y);
                x+=p[i].x;
                y+=p[i].y;
            }
            int m=ConvexHull(p,n,ch);
            double minm=(double)INF,ans;
            if(m<=2) minm=0.0;
            else for(int i=0; i<m; i++)
                 {
                    Line l=GetLine(ch[i],ch[(i+1)%m]);
                    double s=fabs(l.a*x+l.b*y+n*l.c);
                    ans=s/sqrt(l.a*l.a+l.b*l.b);
                    minm=min(ans,minm);
                 }
            printf("Case #%d: %.3lf
    ",ca++,minm/n);
        }
        return 0;
    }
  • 相关阅读:
    学习些新东西
    浏览器内的web开发工具
    基于oracle开发的初步接触
    LAMP3 PHP安装
    svn for windows
    PHP替换掉字符串中的非字符
    搭个邮件服务器
    centos下安装mysql
    安装tomcat
    c#线程
  • 原文地址:https://www.cnblogs.com/54zyq/p/3232024.html
Copyright © 2011-2022 走看看