UVA 11168 - Airport 凸包

本文介绍了一个寻找平面内多个点到一条直线最小平均距离的问题,并给出了详细的解决方案。通过构建凸包并利用几何特性找到最优直线,使得所有点到该直线的距离之和最小。

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

点击打开链接

 

Problem D
Airport
Input:
Standard Input

Output: Standard Output

 

It is no coincidence  that in no known language
  does the phrase 'As pretty as an Airport' appear.

Douglas Adams

There is a small town with n houses. The town needs an airport. An airport is basically a very long, very straight road. Think of it as an infinite line. We need to build the airport such that the average distance from each house to the airport is as small as possible. However, no one wants to walk across the runway, so all of the houses must be on the same side of the airport. (Some houses may be a distance of zero away from the runway, but that's ok; we'll give them some free ear plugs.)

Where should we build the airport, and what will be the average distance?

Input
The first line of input gives the number of cases, N (<=65) .N test cases follow. Each one is a line containing n (0<  n <= 10000) , followed by n lines giving the xy-coordinates of the houses. All coordinates are integers with absolute value of at most 80,000 .

 

Output

For each test case, output one line containing "Case #x:" followed by the average distance from the airport to the houses, with 3 digits after the decimal point. No answer will be within 10-5 of a round-off error case.

 

Sample Input                             Output for Sample Input

 

4
4
0 0
0 1
1 0
1 1
2
15035 39572
34582 39535
3
0 0
0 1
1 0
5
0 0
0 2
2 0
2 2
1 1
Case #1: 0.500
Case #2: 0.000
Case #3: 0.236
Case #4: 1.000

 


Problemsetters: Igor Naverniouk and Derek Kisman

 

 

题意:给出平面上n个点,找一条直线,使得所有的点在直线的同侧(也可以在直线上),且到直线的距离之和尽量小。

A了一天,WA了十多次,终于过了。不容易啊。要选择这么一条直线,不难发现,选择凸包的边所在的直线是最优。由于凸包上的边不超过n条,则只需O(n)时间就会解决这个问题。设直线的一般式方程为Ax+By+C=0,则点(x0,y0)到直线的距离是为:|Ax0+By0+C|/sqrt(A^2+B^2)。因为所有的点在同一侧,所有的点的正负号相同。这样我们先处理所有点的x坐标和y坐标之和。首先要注意定义无穷大的时候不能定义成999999,如果这样会是WA。而要定义成0x3f3f3f3f。然后要注意unique的使用,要去重。

 

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double inf=0x3f3f3f3f;
int n;
double sumx,sumy;
const double eps = 1e-10;
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}//构造函数
    bool operator < (const Point& a) const
	{
		if(a.x != x) return x < a.x;
		return y < a.y;
	}
};
typedef Point Vector;
Point P[10010],ch[10010];

//向量+向量=向量,点+向量=点
Point operator+(Point A,Point B)
{
    return Point(A.x+B.x,A.y+B.y);
}

//点-点=向量
Point operator-(Point A,Point B)
{
    return Point(A.x-B.x,A.y-B.y);
}
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;
}
//就算OA和OB的叉积
double Cross(Point A,Point B)
{
    return A.x*B.y-A.y*B.x;
}


//凸包
int ConvexHull(Point *p,int n,Point *ch)//ch是空的
{
    sort(p,p+n);//x按照从小到大排序,若x相等,按照y从小到大排序
    n=unique(p,p+n)-p;
    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;//返回的是凸包的顶点数,ch数组存的是凸包的顶点
}

//总的点到Ax+By+C的距离
double get(double A,double B,double C)
{
    double k=fabs(A*sumx+B*sumy+n*C);
    double v=sqrt(A*A+B*B);
    return k/v;
}
//Ax+By+C=0
double getDist(Point a,Point b)
{
    double A=a.y-b.y;
    double B=b.x-a.x;
    double C=a.x*b.y-a.y*b.x;
    return get(A,B,C);
}

int main()
{
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        sumx=0,sumy=0;
        double temp,minn=inf;
        scanf("%d",&n);
       for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&P[i].x,&P[i].y);
            sumx+=P[i].x;
            sumy+=P[i].y;

        }
        printf("Case #%d: ",cas++);
        if(n<=2)
        {
            printf("0.000\n");
            continue;
        }
        int m=ConvexHull(P,n,ch);

        for(int i=0;i<m;i++)
        {
            //printf("%lf %lf      %lf %lf\n",ch[i].x,ch[i].y,ch[i+1].x,ch[i+1].y);
            temp=getDist(ch[i],ch[(i+1)%m]);
            //printf("其余点点到i和i+1的距离是:%lf  %lf\n",temp,temp/(nn*1.0));
            minn=min(minn,temp);
        }
        double ans=minn/n;
        printf("%.3lf\n",ans);
    }
    return 0;
}


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值