1038: [ZJOI2008] Watchtower time limit: ten Sec Memory Limit: 162 MB
Submit: 973 Solved: 428
[Submit] [Status] Description
Committed to the construction of a national model of the village of village village Dadzhi H, decided to establish a lookout tower in the village, in order to strengthen the village law and order. We abstract the H village into a one-dimensional contour. As shown we can use the top contour polyline of a mountain (x1, y1), (x2, y2), .... (Xn, yn) to describe the shape of H village, here X1 < X2 < ...< xn. The Watchtower can be built anywhere between [X1, xn], but must meet any location from the top of the Watchtower to see H village. It can be seen that building watchtower at different locations requires different heights. To save money, the Dadzhi mayor wants to build a tower as small as possible. Please write a program to help Dadzhi village head to calculate the minimum height of the tower.
Input
The first line contains an integer n, which represents the number of nodes in the contour polyline. Next the first row n integers, for x1 ~ xn. The third line, n integers, is y1 ~ yn.
Output
Contains only one real number, which is the minimum height of the tower and is exactly three digits after the decimal point.
Sample Input"Input Sample One"
6
1 2 4 5 6 7
1 2 2 4 2 1
"Input Sample Two"
4
10 20 49 59
0 Ten 0Sample Output"Output Example One"
1.000
"Output Example II"
14.500HINT
For 100% of data, n≤300, the absolute value of input coordinates is not more than 106, pay attention to the problem caused by real error.
Half-plane intersection. (It feels like a feasible field in linear programming in mathematics.)
I to i+1 the point that can be seen is the upper area of their connection, for each I and i+1 to make such a line, eventually all points can see the area of the tower must be in this area.
It is easy to prove that the optimal solution is only above the known point, or at the vertex of the feasible domain.
Simply say the method of semi-planar intersection:
1. Sort by polar angle, same as the y-axis intersection
2. For the same line of the polar angle, keep only one
3. Scan each line sequentially, rejecting the line that is definitely not necessary, depending on the horizontal axis of the intersection
4. The remainder is the contour line that makes up the half-plane intersection.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include < cstring> #include <cmath> #define M 305#define eps 1e-9using namespace std;struct point{double x, y;} A[m];struct line{point a,b;double k,b;void getkb () {k= (A.Y-B.Y)/(a.x-b.x); b=a.y-k*a.x;}} L[m],s[m];int n,r;double Ans;bool cmp (line A,line b) {if (Fabs (A.K-B.K) <eps) return A.b<b.b;return A.K<B.K;} Double Getjiao (line A,line b) {return (B.B-A.B)/(A.K-B.K);} Double BP (double x)//half plane intersection {double ans=0.0;for (int i=1;i<=r;i++) Ans=max (ans,s[i].k*x+s[i].b); return ans;} double yz ( Double x)//known point {int i;for (i=1;i<=n;i++) if (i==n| | a[i+1].x>=x) break;if (i==n) return-(1E10); Line Now;now. A=a[i],now. B=a[i+1],now. getKB (); return now.k*x+now.b;} void Getans () {ans=1e10;for (int i=1;i<=n;i++) ans=min (ANS,BP (a[i].x)-a[i].y); for (int. i=1;i<r;i++) {Point p;p.x= Getjiao (s[i],s[i+1]);p. Y=s[i].k*p.x+s[i].b;ans=min (Ans,p.y-yz (p.x));}} int main () {scanf ("%d", &n); for (int i=1;i<=n;i++) scanf ("%lf", &a[i].x), for (Int. i=1;i<=n;i++) scanf ("%lf", &a[i].y); for (int i=1;i<n;i++ ) L[i]. A=a[i],l[i]. B=a[i+1],l[i]. getKB (); sort (l+1,l+n,cmp); r=0;for (int i=1;i<n;i++) if (i==n-1| | Fabs (L[I].K-L[I+1].K) >eps) {while (R>=2&&getjiao (L[i],s[r]) <getjiao (s[r],s[r-1])) r--;s[++r]=l[i ];} Getans ();p rintf ("%.3lf\n", ans); return 0;}
"Bzoj 1038" [ZJOI2008] Lookout tower