Ultraviolet A 11168

Source: Internet
Author: User

Question: There are N points. Find a straight line so that all points are on the same side of a straight line (or in a straight line), and the distance to the straight line is the smallest. The ratio of the output distance to the number of points.
Question: If all vertices are located on the same side of a straight line, that is, the straight line cannot pass through the convex hull, the straight line where the convex hull edge is located is a possible solution. Point (x0, y0) distance to a straight line (AX + by + c = 0)
Dis = | ax0 + by0 + c |/SQRT (a ^ 2 + B ^ 2)
Calculate the distance and distance between all vertices. Based on the formula, calculate the sum of the X and Y coordinates of all vertices to reduce the time complexity.

# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; const double Pi = ACOs (-1); const int n = 10005; const double INF = 1e9; struct point {Double X, Y; point (double x = 0, Double Y = 0): x (x), y (y) {} p [N], Res [N]; struct circle {point C; Double R; Circle () {} circle (point c, double r = 0 ): C (c), R (r) {} Point (double A) {return point (c. X + cos (a) * R, C. Y + Sin (a) * C. y) ;}}; int N; double sqr (Double X) {return x * X;} point operator + (point a, point B) {return point (. X + B. x,. Y + B. y);} point operator-(point a, point B) {return point (. x-B. x,. y-B. y);} point operator * (point a, Double P) {return point (. x * P,. y * P);} point operator/(point a, Double P) {return point (. x/P,. y/P);} // calculate the positive and negative values of the dot product. The angle between the two values is an empty angle int DCMP (Double X) {If (FABS ). (X) <1e-9) return 0; return x <0? -1: 1;} bool operator <(const point & A, const point & B) {return. x <B. X | (. X = B. X &. Y <B. y);} bool operator = (const point & A, const point & B) {return DCMP (. x-B. x) = 0 & DCMP (. y-B. y) = 0;} // calculate the dot product double dot (point a, point B) {return. x * B. X +. y * B. y;} // calculate the cross product, that is, the number product double cross (point a, point B) {return. x * B. y-. y * B. X ;}// returns the vector length double length (point a) {return SQRT (dot (A, A);} // vector A rotates the rad radian, the negative value of rad is clockwise rotation point rotate (point a, double rad) {return point (. x * Cos (RAD)-. y * sin (RAD),. x * sin (RAD) +. y * Cos (RAD);} // converts degrees to radians double torad (double deg) {return deg/180.0 * PI;} int convexhull (point * P, Int & CNT, point * res) {sort (p, p + CNT); CNT = unique (p, p + CNT)-P; int m = 0; For (INT I = 0; I <CNT; I ++) {While (M> 1 & cross (RES [m-1]-res [m-2], P [I]-res [m-2]) <= 0) m --; Res [M ++] = P [I];} int K = m; for (INT I = CNT-2; I> = 0; I --) {While (M> K & cross (RES [m-1]-res [m-2], p [I]-res [m-2]) <= 0) M --; Res [M ++] = P [I];} If (CNT> 1) m --; return m;} void change (double & A, double & B, double & C, point A, point B) {A = B. y-. y; B =. x-B. x; C = B. x *. y-. x * B. y;} int main () {int T, CAS = 1; scanf ("% d", & T); While (t --) {scanf ("% d ", & N); double sumx = 0, Sumy = 0; For (INT I = 0; I <n; I ++) {scanf ("% lf ", & P [I]. x, & P [I]. y); sumx + = P [I]. x; Sumy + = P [I]. y;} If (n <= 2) {printf ("case # % d: 0.000 \ n", CAS ++); continue;} int temp = convexhull (p, n, Res); double Minn = inf; For (INT I = 0; I <temp; I ++) {double A, B, C; Change (A, B, c, res [I], Res [(I + 1) % TEMP]); double sumdis = FABS (A * sumx + B * Sumy + C * n) /SQRT (sqr (A) + sqr (B); Minn = min (Minn, sumdis);} printf ("case # % d: %. 3lf \ n ", CAS ++, Minn/n);} return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Ultraviolet A 11168

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.