UVA11168 Airport

Posted autoint

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11168 Airport相关的知识,希望对你有一定的参考价值。

题意

PDF

分析

首先发现距离最短的直线肯定在凸包上面。

然后考虑直线一般方程(Ax+By+C=0),点((x_0,y_0))到该直线的距离为
[ frac{|Ax_0+By_0+C|}{sqrt{A^2+B^2}} ]
由于所有点在直线同侧,所以绝对值里面的符号相同,所以维护所有点(x)坐标和(y)坐标之和就行了。

时间复杂度(O(T n log n))

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<ctime>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read()
{
    rg T data=0;
    rg int w=1;
    rg char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-')
            w=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        data=data*10+ch-'0';
        ch=getchar();
    }
    return data*w;
}
template<class T>T read(T&x)
{
    return x=read<T>();
}
using namespace std;
typedef long long ll;

struct Point
{
    double x,y;
    
    Point(double x=0,double y=0)
    :x(x),y(y){}
    
    bool operator<(co Point&rhs)
    {
        return x<rhs.x||(x==rhs.x&&y<rhs.y);
    }
    
    bool operator==(co Point&rhs)
    {
        return x==rhs.x&&y==rhs.y;
    }
};
typedef Point Vector;

Vector operator-(co Point&A,co Point&B)
{
    return Vector(A.x-B.x,A.y-B.y);
}

double Cross(co Vector&A,co 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;
}

// (x2-x1)(y-y1) = (y2-y1)(x-x1) -> ax+by+c=0
void LineGeneralEquation(co Point&p1,co 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()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    int T=read<int>();
    for(int kase=1;kase<=T;++kase)
    {
        int n=read<int>();
        vector<Point>P;
        double sumx=0,sumy=0;
        for(int i=0;i<n;++i)
        {
            int x=read<int>(),y=read<int>();
            sumx+=x,sumy+=y;
            P.push_back(Point(x,y));
        }
        vector<Point>ch=ConvexHull(P);
        int m=ch.size();
        double ans=1e9;
        if(m<=2)
            ans=0;
        else
            for(int i=0;i<m;++i)
            {
                double a,b,c;
                LineGeneralEquation(ch[i],ch[(i+1)%m],a,b,c);
                ans=min(ans,fabs(a*sumx+b*sumy+c*n)/sqrt(a*a+b*b));
            }
        printf("Case #%d: %.3lf
",kase,ans/n);
    }
    return 0;
}

以上是关于UVA11168 Airport的主要内容,如果未能解决你的问题,请参考以下文章

UVA–11168 Airport[凸包]

UVa 11168(凸包直线一般式)

UVA11374 Airport Express

UVA11374 Airport Express

Uva11374 Airport Express

UVA11374 Airport Express