Topic link
Analysis
See through the problem
Considering every line segment that makes up the village, it is clear that we will see it in the half plane above the line where the line is located. Therefore, watchtower must be in the intersection of the half plane above the line where all the line segments of the village are formed, in order to
From the top of watchtower, you can see anywhere in H village.
Therefore, this problem is to seek the shortest distance from the ground of the village to this half plane. Implementation Mode
A lot of students see the half plane intersection feel code must be very high (e) End (Xin). But the half plane of the problem is very special.
Let's take a look at this example first. Example
Example Link: "Computational geometry" [hnoi2008][hysbz/bzoj1007] Horizontal visible Line
Examples and the half plane of the problem are above the line.
The example is to find out which line is in the line that makes up the half plane intersection. back to the problem . Specific Approach
This problem can also be used in the same way to find a half plane intersection.
The half plane intersection and the ground are all line segments, which can be regarded as piecewise functions.
The function that makes the half plane intersection is f (x) f (x), and the ground is g (x) g (x).
H (x) =f (x) −g (x) h (x) =f (x)-G (x)
Ans=min (H (x)) Ans=min (H (x))
Consider this f (x), g (x) Two piecewise functions, each of which is a single function, and H (x) is clearly a function, and the dividing point of the segment is clearly the dividing point of the F (x), g (x) two functions.
The extremum of a function is obviously obtained at the endpoint (the boundary point).
Bingo
We only need to enumerate the boundaries of f (x), g (x), and then compute the value of h (x) at the dividing point, which requires only the time complexity of O (n) o (n).
Total time complexity O (NLOGN) O (nlogn) (sorted time complexity) code
#include <cstdio> #include <algorithm> #include <cmath> #define MAXN by using namespace std;
#define EPS 1e-9 #define INF 1e27 struct node{double k,b;
BOOL operator< (const node &x) const{if (Fabs (K-X.K) <eps) return b>x.b;
Return k<x.k;
BOOL operator== (const node &x) const{return fabs (K-X.K) <EPS;
}}A[MAXN+10],B[MAXN+10];
struct point{double k,b,x,y;
Point () {};
Point (node a,double xx,double yy) {x=xx,y=yy,k=a.k,b=a.b;
}}P[MAXN+10];
int N,X[MAXN+10],Y[MAXN+10],NA,NP;
Double Ans=inf;
void Read (int &x) {char C;
BOOL f=0;
while (C=getchar (), c!=eof) {if (x== '-') f=1;
if (c>= ' 0 ' &&c<= ' 9 ') {x=c-' 0 ';
while (C=getchar (), c>= ' 0 ' &&c<= ' 9 ') x=x*10+c-' 0 ';
UNGETC (C,stdin);
if (f) x=-x;
Return }} void Read () {int i;
Read (n);
for (i=1;i<=n;i++) Read (X[i]);
for (i=1;i<=n;i++) Read (Y[i]);
for (i=1;i<n;i++) {b[i].k=1.0* (y[i]-y[i+1])/(x[i]-x[i+1]);
B[I].B=Y[I]-X[I]*B[I].K;
A[i]=b[i];
Sort (a+1,a+n);
Na=unique (a+1,a+n)-a-1;
} void Ints_halfplane () {int i,j,ti;
Double tx,x;
for (i=1;i<na;) {x=inf;
for (j=i+1;j<=na;j++) {tx= (a[j].b-a[i].b)/(A[I].K-A[J].K);
if (tx<=x) ti=j,x=tx;
} p[++np]=point (A[I],X,X*A[I].K+A[I].B);
I=ti;
} p[++np]=point (a[na],inf,0);
} void Solve () {int i,j;
Ints_halfplane (); for (i=j=1;i<=np&&j<=n;) {if (P[i].x<x[j]) {ans=min (P[i].y-b[j-1].k*p[i].x-b[j-1].b,ans
);
i++;
} else{Ans=min (P[i].k*x[j]+p[i].b-y[j],ans);
j + +;
int main () {Read ()}}
Solve (); printf ("%.3lf", ans); }