Ultraviolet A 11168 (convex bag + distance from a point to a straight line) Airport

Source: Internet
Author: User
Tags define local

Question:

There are N points on the plane. Find a straight line so that all points are on the same side of the straight line. And find the minimum value of the distance between these points and the straight line.

Analysis:

As long as the straight line does not pass through the convex hull, the first condition is met. To minimize the distance and distance, the straight line must be on the side of the convex hull. After finding the convex hull, enumerate each side and find the sum of the distance from all points to the straight line to get the minimum value.

The formula for distance from a point to a straight line is:

Because the points are on the same side of a straight line, we can "move" the addition to it, and finally obtain the absolute value. Therefore, we can preprocess the sum of the X and Y coordinates of all points. Of course, the constant C must also be multiplied by N times.

We know that the two-point coordinate is used to find the equation of the straight line at this point. This is a good solution. Considering that the straight line does not have a slope, We need to multiply the denominator in the expression.

 

1 // # define local 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include <vector> 7 using namespace std; 8 9 struct point 10 {11 double X, Y; 12 point (double x = 0, Double Y = 0): x (x), y (y) {} 13 }; 14 typedef point vector; 15 point operator + (point a, point B) 16 {17 return point (. X + B. x,. Y + B. y); 18} 19 point operator-(point a, point B) 20 {21 return point (. x-B.x,. y-B.y); 22} 23 bool operator <(const point & A, const point & B) 24 {25 return. x <B. X | (. X = B. X &. Y <B. y); 26} 27 bool operator = (const point & A, const point & B) 28 {29 return. X = B. X &. y = B. y; 30} 31 double cross (vector A, vector B) 32 {33 return. x * B. y-. y * B. x; 34} 35 36 vector <point> convexhull (vector <point> P) {37 // preprocessing, delete repeat 38 sort (P. begin (), P. end (); 39 p. erase (unique (P. begin (), P. end (), p. end (); 40 41 int n = P. size (); 42 int m = 0; 43 vector <point> CH (n + 1); 44 for (INT I = 0; I <n; I ++) {45 while (M> 1 & cross (CH M-1]-ch [m-2], p [I]-ch [m-2]) <= 0) m --; 46 ch [M ++] = P [I]; 47} 48 int K = m; 49 for (INT I = n-2; I> = 0; I --) {50 while (M> K & cross (CH M-1]-ch [m-2], p [I]-ch [m-2]) <= 0) m --; 51 ch [M ++] = P [I]; 52} 53 If (n> 1) m --; 54 // For (INT I = 0; I <m; ++ I) printf ("% lf \ n", CH [I]. x, CH [I]. y); 55 ch. resize (m); 56 return ch; 57} 58 59 double sumx, Sumy; 60 61 double dist (point a, point B, int m) 62 {63 double A =. y-b.y, B = B. x-a.x, c =. x * B. y-B. x *. y; 64 // printf ("% lf", FABS (A * sumx + B * Sumy + C), SQRT (A * A + B * B )); 65 return (FABS (A * sumx + B * Sumy + C * m)/SQRT (A * A + B * B); 66} 67 68 int main (void) 69 {70 # ifdef local 71 freopen ("11168in.txt", "r", stdin); 72 # endif 73 74 int t; 75 scanf ("% d", & T ); 76 for (INT Kase = 1; Kase <= T; ++ Kase) 77 {78 int N; 79 vector <point> P; 80 sumx = 0.0, Sumy = 0.0; 81 scanf ("% d", & N); 82 For (INT I = 0; I <n; ++ I) 83 {84 Double X, Y; 85 scanf ("% lf", & X, & Y); 86 p. push_back (point (x, y); 87 sumx + = x; Sumy + = y; 88} 89 vector <point> CH = convexhull (P ); 90 int M = CH. size (); 91 // For (INT I = 0; I <m; ++ I) printf ("% lf \ n", CH [I]. x, CH [I]. y); 92 If (M <= 2) 93 {94 printf ("case # % d: 0.000 \ n", Kase); 95 continue; 96} 97 98 double ans = 1e10; 99 for (INT I = 0; I <m ;++ I) 100 Ans = min (ANS, dist (CH [I], ch [(I + 1) % m], n); 101 printf ("case # % d: %. 3lf \ n ", Kase, ANS/n); 102} 103}
Code Jun

 

Ultraviolet A 11168 (convex bag + distance from a point to a straight line) 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.