uva 11168 Airport

来源:互联网 发布:魅族云相册软件 编辑:程序博客网 时间:2024/05/11 03:39

原题:
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
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
Sample Output
Case #1: 0.500
Case #2: 0.000
Case #3: 0.236
Case #4: 1.000

中文:
现在要建立一个机场,这个机场看成是一条无限长的直线。现在又n个居民点,所有居民点要在机场的一侧,而且居民点可以在机场的这条线上。现在问你,所有居民点到机场的距离的平均值最小能是多少?

#include <bits/stdc++.h>#define Vector Pointusing namespace std;//fstream in,out;const int maxn=10005;const double PI=acos(-1);double torad(double deg){    return deg/180*PI;}struct Point{    double x,y;    Point(double x=0,double y=0):x(x),y(y){}};bool operator < (const Point &a,const Point &b){    return a.x<b.x||(a.x==b.x&&a.y<b.y);}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 ConvexHull(Point *p,int n,Point *ch){    sort(p,p+n);    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;}double Distance(Point a,Point b,double totx,double toty,int n){    double A=b.y-a.y;    double B=a.x-b.x;    double C=a.y*b.x-b.y*a.x;    double dis=fabs(totx*A+toty*B+n*C)/sqrt(A*A+B*B);    return dis;}int main(){    ios::sync_with_stdio(false);    int t,n,k=1;    Point P[maxn],ch[maxn];    cin>>t;    while(t--)    {        int pc=0;        double ans=INT_MAX;        cin>>n;        double totx=0,toty=0;        for(int i=0;i<n;i++)        {            cin>>P[i].x>>P[i].y;            totx+=P[i].x;            toty+=P[i].y;        }        int m=ConvexHull(P,n,ch);        cout<<"Case #"<<k++<<": ";        if(m<=2)        {            cout<<"0.000"<<endl;            continue;        }        for(int i=1;i<m;i++)            ans=min(ans,Distance(ch[i-1],ch[i],totx,toty,n));        ans=min(ans,Distance(ch[m-1],ch[0],totx,toty,n));//      cout<<ans<<endl;        cout<<fixed<<setprecision(3)<<ans/(n*1.0)<<endl;    }    return 0;}

解答:
很明显这条机场要建立在这些居民点形成的凸包的一条边上面,枚举凸包的每一条边。然后用距离公式求和即可。这里注意,由于所有居民点都在直线的同一侧,所以可以把所有局面点的x坐标值和y的坐标值累加,利用AX+By+C=0的点到直线方程直接求出来。两点式直线方程当中,设a点为(x1,y1),b点为(x2,y2)

k=y1y2x1x2
b=y2x1y1x2x1x2
想要换成标准式,求出A,B,C的值,把式子左右两侧乘以x1x2即可

(y2y1)x+(x1x2)y+y1x2y2x1=0
注意此题如果m=0的时候记得直接判定距离为0~

0 0