凸包,点到直线距离(飞机场,uva 11168)

本文针对一个几何问题,提出从O(n²)到O(n)的算法优化方案,通过预处理和巧妙利用点到直线距离公式,实现计算平均距离的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大白书又没说清楚,明明找的是平均距离,却说是距离和。。。

关于debug,考虑极端数据。

然后我用的是O(n^2)的算法,太糟糕了。

想要算法高效,思路大致都是通过维护某些量或者预处理一些值或者利用公式将某一重循环省掉,但可能需要付出额外的代价,往往是将某个O(n)降为O(longn)或O(1)。

至于该如何去发现这些优化点,我想自己应该多关注那些看上去很邋遢的循环,然后找到待求量的特别之处。

比如在这题中,我们用了一个循环去求距离和,这算法让人感到十分邋遢。而我们要求的距离和与一般的距离和相比,有什么特别之处呢?那就是发现点都在直线的一边,这时如果你对点到直线距离公式有足够深刻的理解的话,那么应该就能很容易想到预处理坐标和,O(1)算距离和的算法了。


大白书上抛出了一个问题:如何把直线的两点式转化为一般式?

设Point P1,Point P2分别为直线上两点。

设Vector V=P2-P1。

那么根据斜截式y=kx+b得:

y=V.y/V.x*x+b     =====>     y*V.x=x*V.y+b*V.x     =====>     x*V.y-y*V.x+b*V.x

将上式与一般式Ax+By+C=0对比得:

A=V.y

B=-V.x

C=b*V.x=y*V.x-x*V.y

A,B已经求出。

将P1或P2点坐标带入(x,y)便可求得C。


O(n^2)代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 10010;

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

typedef Point Vector;

Point ReadPoint()
{
    double x,y;
    scanf("%lf %lf",&x,&y);
    return Point(x,y);
}

const double eps = 1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}

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

bool operator == (Point A,Point B)
{
    return dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)==0;
}

double Dot(Point A,Point B)
{
    return A.x*B.x+A.y*B.y;
}

double Cross(Point A,Point B)
{
    return A.x*B.y-A.y*B.x;
}

double Len(Vector A)
{
    return sqrt(Dot(A,A));
}

double DistToLine(Point P,Point A,Point B)
{
    Vector v1=B-A;
    Vector v2=P-A;
    return fabs(Cross(v1,v2)/Len(v1));
}

int ConvexHull(Point* P,int n,Point* ans)
{
    sort(P,P+n);
    n=unique(P,P+n)-P;
    int m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1&&dcmp(Cross(ans[m-1]-ans[m-2],P[i]-ans[m-2]))<=0) m--;
        ans[m++]=P[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k&&dcmp(Cross(ans[m-1]-ans[m-2],P[i]-ans[m-2]))<=0) m--;
        ans[m++]=P[i];
    }
    if(n>1) m--;
    return m;
}

Point P[maxn];
Point ans[maxn];

double GetDist(int n,Point A,Point B)
{
    double ret=0;
    for(int i=0;i<n;i++)
        ret+=DistToLine(P[i],A,B);
    return ret;
}

int main()
{
    //freopen("data.txt","r",stdin);
    int T,n;
    scanf("%d",&T);
    for(int t=1;t<=T;t++)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            P[i]=ReadPoint();
        int m=ConvexHull(P,n,ans);
        double ANS=DBL_MAX;
        for(int i=0;i<m;i++)
            ANS=min(ANS,GetDist(n,ans[i],ans[(i+1)%m]));
        if(n==1) ANS=0;
        printf("Case #%d: %.3lf\n",t,ANS/n);
    }
    return 0;
}

O(n)代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 10010;

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

typedef Point Vector;

Point ReadPoint()
{
    double x,y;
    scanf("%lf %lf",&x,&y);
    return Point(x,y);
}

const double eps = 1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}

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

bool operator == (Point A,Point B)
{
    return dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)==0;
}

double Dot(Point A,Point B)
{
    return A.x*B.x+A.y*B.y;
}

double Cross(Point A,Point B)
{
    return A.x*B.y-A.y*B.x;
}

double Len(Vector A)
{
    return sqrt(Dot(A,A));
}

int ConvexHull(Point* P,int n,Point* ans)
{
    sort(P,P+n);
    n=unique(P,P+n)-P;
    int m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1&&dcmp(Cross(ans[m-1]-ans[m-2],P[i]-ans[m-2]))<=0) m--;
        ans[m++]=P[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k&&dcmp(Cross(ans[m-1]-ans[m-2],P[i]-ans[m-2]))<=0) m--;
        ans[m++]=P[i];
    }
    if(n>1) m--;
    return m;
}

struct Line
{
    double A,B,C;
    Line(Point P1,Point P2)
    {
        Vector V=P2-P1;
        A=V.y;
        B=-V.x;
        C=V.x*P1.y-V.y*P1.x;
    }
};

Point P[maxn];
Point ans[maxn];
double sumx,sumy;

double GetDist(int n,Line L)
{
    return fabs(L.A*sumx+L.B*sumy+L.C*n)/sqrt(L.A*L.A+L.B*L.B);
}

int main()
{
    //freopen("data.txt","r",stdin);
    int T,n;
    scanf("%d",&T);
    for(int t=1;t<=T;t++)
    {
        sumx=sumy=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            P[i]=ReadPoint();
            sumx+=P[i].x;
            sumy+=P[i].y;
        }
        int m=ConvexHull(P,n,ans);
        double ANS=DBL_MAX;
        for(int i=0;i<m;i++)
            ANS=min(ANS,GetDist(n,Line(ans[i],ans[(i+1)%m])));
        if(n==1) ANS=0;
        printf("Case #%d: %.3lf\n",t,ANS/n);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值