Topic Portal
Test instructions: The small island of the convex polygon in the sea, ask the island's point to the sea farthest distance.
Analysis: Training Guide P279, two-point answer, and then the entire polygon to internal contraction, if the half-plane intersection non-empty, then these points constitute a half-plane, there are satisfied points.
/************************************************* author:running_time* Created time:2015/11/10 Tuesday 14:16:17* Fi Le Name:LA_3890.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) {//three state function, reduced accuracy problem if (fabs (x) < EPS) return 0; else return x < 0? -1:1;} struct Point {//points defined by double x, y; Point () {}-point (Double x, double y): X (x), Y (y) {}-operator + (const point &r) const {//vector addition return point (x + r.x, y + r.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); } BOOL operator < (const point &r) const {//DOT coordinate sort return x < R.x | | (x = = r.x && y < r.y); } bool operator = = (Const point &r) const {//judgment same dot return dcmp (x-r.x) = = 0 && dcmp (y-r . y) = = 0; }};typedef Point Vector; Vector definition point read_point (void) {//points read in 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 a) {//vector length, dot product return sqrt (dot (A, a));} Double Polar_angle (vector A) {//Vector polar return atan2 (A.Y, a.x);} Vector Nomal (vector a) {//vector double len = length (a) of the unit method vectors; Return Vector (-a.y/len, A.x/len);} struct line {point P; Vector v; Double ang; LinE () {} line (const point &p, const Vector &v): P (P), V (v) {ang = Polar_angle (v); } BOOL operator < (const line &R) const {return Ang < R.ang; } Point point (Double A) {return P + v * A; }};bool Point_on_left (point P, line L) {return cross (L.V, P-L.P) > 0;} Point Line_line_inter (point P, Vector V, point q, Vector W) {//two line intersection, parametric equation Vector U = p-q; Double T = Cross (w, U)/Cross (V, W); return p + V * t;} Vector<point> Half_plane_inter (vector<line> L) {sort (L.begin (), L.end ()); int first, last, n = l.size (); Point *p = new Point[n]; Line *q = new Line[n]; Q[first=last=0] = l[0]; for (int i=1; i<n; ++i) {When (First < last &&!point_on_left (p[last-1], l[i])) last--; while (first < last &&!point_on_left (P[first], l[i])) first++; Q[++last] = L[i]; if (dcmp (Cross (Q[LAST].V, q[last-1].v)) = = 0) { last--; if (Point_on_left (L[I].P, Q[last])) q[last] = L[i]; } if (first < last) p[last-1] = Line_line_inter (Q[LAST-1].P, Q[LAST-1].V, Q[LAST].P, Q[LAST].V); } while (First < last &&!point_on_left (p[last-1], Q[first])) last--; Vector<point> PS; if (Last-first <= 1) return PS; P[last] = Line_line_inter (Q[LAST].P, Q[LAST].V, Q[FIRST].P, Q[FIRST].V); for (int i=first; i<=last; ++i) Ps.push_back (P[i]); return PS;} Point ps[110]; Vector v[110], v2[110];int main (void) {int n; while (scanf ("%d", &n) = = 1) {if (!n) break; for (int i=0; i<n; ++i) ps[i] = Read_point (); for (int i=0; i<n; ++i) {V[i] = ps[(i+1)%n]-ps[i]; V2[i] = Nomal (V[i]); } Double L = 0, r = 20000; while (R-l > EPS) {Double mid = L + (r-l)/2; Vector<line> L; for (int i=0; i<n; ++i) { L.push_back (Line (Ps[i] + v2[i] * Mid, v[i])); } vector<point> qs = Half_plane_inter (L); int sz = Qs.size (); if (sz = = 0) R = Mid; else L = mid; } printf ("%.6f\n", L); }//cout << "time Elapsed:" << 1.0 * Clock ()/clocks_per_sec << "s.\n"; return 0;}
Simple geometry (half-plane intersection + dichotomy) LA 3890 Most distant points from the Sea