UVA 11168 Airport

Source: Internet
Author: User
Tags min

Original question:
There is a small town with n houses. The town of 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 the
Average distance from all house to the airport are as small as possible. However, no one wants to walk across the runway, so all of the houses must is on the same side of the airport. (Some houses is a distance of zero away from the runway, and that's OK; we ll give them Some free ear plugs.) Where should we build the airport, and what would 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 is integers with absolute value of in 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 would be within 10−5 of a Round-off the 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

English:
Now to build an airport, the airport is regarded as an infinitely long line. Now there are n settlements, all the settlements are on one side of the airport, and the settlements can be on this line of the airport. Now ask you, what is the minimum of the average distance to the airport for all residents?

#include <bits/stdc++.h> #define Vector point using 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&AMP;&AMP;A.Y&LT;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; }

Answer:
It's obvious that the airport will be built on one side of the convex hull formed by these settlements, enumerating each side of the convex hull. Then you can use the distance formula to sum. Note here that since all settlements are on the same side of the line, it is possible to accumulate the x-coordinate values of all the points and the coordinates of Y, using the ax+by+c=0 point-to-line equation to find out directly. Two-point linear equation, set a point is (x1,y1), B is (x2,y2)

Slope k=y1−y2x1−x2 Slope K=\frac{y_1-y_2}{x_1-x_2}
Intercept b=y2x1−y1x2x1−x2 intercept b=\frac{y_2 x_1-y_1 x_2}{x_1-x_2}
Want to change the standard formula, find out the value of A,b,c, Bashi the left and right sides multiplied by x1−x2 X_1-x_2 can

(y2−y1) x+ (x1−x2) y+y1x2−y2x1=0 (y_2-y_1) x+ (x_1-x_2) y+y_1x_2-y_2x_1=0
Note that if m=0, remember to determine the distance is 0~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.