Simple geometry (mathematical formula + convex hull) UVA 11168 Airport

Source: Internet
Author: User

Topic Portal

Test instructions: Find a straight line, so that the rest of the points are in the same side of the line, and so that the average distance to get straight lines shortest.

Analysis: The training guide P274, the convex hull first, if each side is counted side, is O (n ^ 2), however, according to the formula known straight line ax + by + C = 0. The distance from the point (x0, y0) to the line is: Fabs (ax0+by0+c)/sqrt (a*a+ B*B).

So as long as the x's and and Y's and, can be calculated at O (1) all distances and.

The two-point linear equation P1 (x1, y1), p2 (x2, y2) is converted to A general linear equation: A = y1-y2, B = x2-x1, C =-A * x1-b * y1;

/************************************************* author:running_time* Created time:2015/11/10 Tuesday 11:24:09* Fi Le Name:UVA_11168.cpp ************************************************/#include <bits/stdc++.h>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 1e5 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const Double EPS = 1e-10;c    Onst Double PI = ACOs ( -1.0), int dcmp (double x) {if (Fabs (x) < EPS) return 0; else return x < 0? -1:1;}    struct point {double x, y;        Point () {} A (double x, double y): X (x), Y (y) {} point operator-(const point &r) const {//Vector subtraction    Return point (X-r.x, Y-R.Y);    } Point operator * (double p) const {//vector multiplied by a scalar return point (x * p, Y * p);    } point operator/(double p) const {//vector divided by scalar return point (x/p, y/p); } point operator + (const-point & r) Const {return point (x + r.x, y + r.y); } BOOL operator < (const point &r) const {return x < r.x | |    (x = = r.x && y < r.y);    } bool operator = = (Const point &r) const {return dcmp (x-r.x) = = 0 && dcmp (y-r.y) = = 0; }};typedef Point Vector;    Point Read_point (void) {double x, y;    scanf ("%lf%lf", &x, &y); Return point (x, y);} Double dot (vector A, vector B) {//vector dot product return a.x * b.x + a.y * B.Y;} Double Cross (vector A, vector B) {//vector fork product return a.x * B.Y-A.Y * b.x;} Double Length (Vector v) {return sqrt (dot (v, v));}    Vector<point> convex_hull (vector<point> PS) {sort (Ps.begin (), Ps.end ());    Ps.erase (Unique (Ps.begin (), Ps.end ()), Ps.end ());    int n = ps.size (), k = 0;    Vector<point> QS (n * 2);         for (int i=0; i<n; ++i) {when (K > 1 && Cross (qs[k-1]-qs[k-2], Ps[i]-qs[k-2]) <= 0) k--; qs[k++] = ps[I]; } for (int t=k, i=n-2; i>=0;-I.) {while (K > t && Cross (Qs[k-1]-qs[k-2], Ps[i]-qs[k-2]) &lt        ; = 0) k--;    qs[k++] = ps[i];    } qs.resize (k-1); return QS;} Double Sqr (double x) {return x * x;}  int main (void) {int T, cas = 0;    scanf ("%d", &t);  while (t--) {int n;        scanf ("%d", &n);        Vector<point> PS;        Double x, y, sx = 0, sy = 0;            for (int i=0; i<n; ++i) {scanf ("%lf%lf", &x, &y);    SX + = x;            sy + = y;        Ps.push_back (Point (x, y));        } vector<point> qs = Convex_hull (PS);    if (int) qs.size () <= 2) {printf ("Case #%d:%.3f\n", ++cas, 0.0);        Continue        } double mn = 1e9;        Qs.push_back (Qs[0]);            for (int i=0; i<qs.size ()-1; ++i) {Double A = qs[i].y-qs[i+1].y;            Double B = qs[i+1].x-qs[i].x;    Double C =-A * qs[i].x-b * QS[I].Y;        Double tmp = fabs (A * SX + B * sy + n * C)/sqrt (SQR (a) + SQR (B));        if (MN > tmp) MN = tmp;    } printf ("Case #%d:%.3f\n", ++cas, mn/n);    }//cout << "time Elapsed:" << 1.0 * Clock ()/clocks_per_sec << "s.\n"; return 0;}

  

Simple geometry (mathematical formula + convex hull) UVA 11168 Airport

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.