[BZOJ1038][ZJOI2008] Watchtower (half plane intersection)

1038: [ZJOI2008] Watchtower

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2999  Solved: 1227
[Submit][Status][Discuss]

Description

  Dadzhi, the head of village H, who is committed to building a national demonstration village of harmony, decided to build a watchtower in the village to strengthen the security of the village. We
abstract the H-village as a one-dimensional contour. As shown in the figure below, we can
describe the shape of H village with the upper contour polylines (x1, y1), (x2, y2), …. (xn, yn) of a mountain, where x1 < x2 < …< xn. The watchtower can be built at any position between [x1, xn], but it must meet
any position where the village H can be seen from the top of the watchtower. It can be seen that the construction of watch towers in different locations requires different heights. In order to save money, the village chief of dadzhi
wanted to build the tower as small as possible. Please write a program to help dadzhi village chief calculate the minimum height of the tower.

Input

  The first line contains an integer n representing the number of nodes of the contour polyline. Next, the first line contains n integers, which are x1 ~ xn. The third line contains n integers, which are y1
 ~ yn.

Output

  Contains only one real number, the minimum height of the tower, accurate to three decimal places.

Sample Input

【Input example 1】
6
1 2 4 5 6 7
1 2 2 4 2 1
【Input example 2】
4
10 20 49 59
0 10 10 0

Sample Output

【Example 1】
1.000
【Example 2】
14.500

HINT

 N ≤ 300, the absolute value of the input coordinates does not exceed 106, pay attention to consider the problems caused by real number errors.

Source

[ Submit ][ Status ][ Discuss ]

This is really done. . I'm exhausted. . In fact, it's a half-plane intersection, but I found that I actually don't know this thing at all.

It is said that both simulated annealing and three-pointing can be done, but consider intersecting the upper half of each edge, and finally the point on the convex hull and the point of the original polyline may be the answer.

Proofs that should be the extremes of a piecewise linear function are only possible at the endpoints.

The rest is a series of half-plane intersection templates. I wrote a function that first converts the two-point formula into a point-slope straight line equation and then finds the intersection. WA, I found that the point-slope formula can't handle a straight line parallel to the y-axis at all. .

Then I looked at the definite ratio point cross product intersection function in the previous template, WA, and found that this can only find the intersection of line segments.

https://blog.csdn.net/u013050857/article/details/40923789

reluctantly at last

 

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #define rep(i,l,r) for (int i=l; i<=r; i++)
 5 typedef double db;
 6 using namespace std;
 7 
 8 const int N=1010;
 9 const double eps=1e-8;
10 int n,tot,cnt;
11 db ans=1e60;
12 struct P{ db x,y; }p[N],a[N];
13 struct L{ P a,b; db sl; }l[N],q[N],tmp[N];
14 
15 double dmult(P a,P b,P c){ return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); }
16 bool operator <(const L &a,const L &b){ return (a.sl!=b.sl) ? a.sl<b.sl : dmult(a.a,a.b,b.b)<-eps; }
17 
18 P inter(L a,L b){
19     double A1=a.b.y-a.a.y,B1=a.a.x-a.b.x,C1=-B1*a.a.y-A1*a.a.x;
20     double A2=b.b.y-b.a.y,B2=b.a.x-b.b.x,C2=-B2*b.a.y-A2*b.a.x;
21     return (P){(C2*B1-C1*B2)/(A1*B2-A2*B1),(C1*A2-C2*A1)/(A1*B2-A2*B1)};
22 }
23 bool jud(L a,L b,L c){ P t=inter(a,b); return dmult(c.a,c.b,t)<-eps; }
24 
25 void work(){
26     tmp[++tot]=l[1];
27     rep(i,2,cnt) if (fabs(l[i].sl-l[i-1].sl)>eps) tmp[++tot]=l[i];
28     rep(i,1,tot) l[i]=tmp[i];
29     int L=1,R=0; q[++R]=l[1]; q[++R]=l[2];
30     rep(i,3,tot){
31         while (L<R && jud(q[R-1],q[R],l[i])) R--;
32         while (L<R && jud(q[L+1],q[L],l[i])) L++;
33         q[++R]=l[i];
34     }
35     while (L<R && jud(q[R-1],q[R],q[L])) R--;
36     while (L<R && jud(q[L+1],q[L],q[R])) L++;
37     tot=0; rep(i,L,R-1) p[++tot]=inter(q[i],q[i+1]);
38 }
39 
40 void getans(){
41     rep(k,1,tot)
42         rep(i,1,n-1){
43             P t=(P){p[k].x,-1};
44             if (p[k].x>=a[i].x && p[k].x<=a[i+1].x)
45                 ans=min(ans,p[k].y-inter((L){a[i],a[i+1]},(L){t,p[k]}).y);
46         }
47     rep(k,1,n)
48         rep(i,1,tot-1){
49             P t=(P){a[k].x,-1};
50             if (a[k].x>=p[i].x && a[k].x<=p[i+1].x)
51                 ans=min(ans,inter((L){p[i],p[i+1]},(L){t,a[k]}).y-a[k].y);
52         }
53 }
54 
55 int main(){
56     freopen("tower.in","r",stdin);
57     freopen("tower.out","w",stdout);
58     scanf("%d",&n);
59     rep(i,1,n) scanf("%lf",&a[i].x);
60     rep(i,1,n) scanf("%lf",&a[i].y);
61     a[0]=(P){a[1].x,100000}; a[n+1]=(P){a[n].x,100000};
62     rep(i,0,n) l[++cnt]=(L){a[i],a[i+1],atan2(a[i+1].y-a[i].y,a[i+1].x-a[i].x)};
63     sort(l+1,l+cnt+1); work(); getans(); printf("%.3lf\n",years);
64      return  0 ;
65 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325066153&siteId=291194637