bzoj1038 ZJOI2008 Lookout Tower __bzoj

Source: Internet
Author: User

1038: [ZJOI2008] Lookout tower Time Limit:10 Sec Memory limit:162 MB
submit:1628 solved:709
[Submit] [Status] [Discuss] Description

Dadzhi, who is committed to building a national model of the village village of H Village, decided to build a watchtower in the village to strengthen law and order in the village. We
The H-Village is abstracted into a one-dimensional contour. As shown in the following figure we can use the top contour line of a mountain (x1, y1), (x2, y2), .... (Xn, yn) to trace
described the shape of H village, here X1 < X2 < ...< xn. Watchtower can be built anywhere between [X1, xn], but must be met from the top of watchtower
To see any place in H village. It can be seen that building watchtower in different locations requires a different height. In order to save money, Dadzhi village chief
Hope to build the tower height as small as possible.  Please write a program to help Dadzhi village chief calculate the minimum height of the tower. Input

The first row contains an integer n that represents the number of nodes for the contour polyline. Next the first row n integers, for x1 ~ xn. Third row n integers, for y1
~ yn. Output

Contains only one real number, the minimum height of the tower, accurate to 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 0 Sample output "Sample One"
1.000
"Output Sample Two"
14.500 HINT

n≤300, the absolute value of the input coordinate is not more than 106, and the problems caused by the real error are considered.




First, there is a conclusion that the maximum value of a piecewise function can only be taken at the boundary and inflection point. (Obviously ...)

So we can first find the half plane intersection, then enumerate all inflection points and boundary point, calculate the minimum value.




#include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring > #include <algorithm> #define F (i,j,n) for (int i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) #
Define LL long #define MAXN 1005 #define EPS 1e-8 #define INF 1000000000 using namespace std;
int N,cnt,tot;
Double Ans=1e60; struct p{double x,y;}
P[MAXN],A[MAXN]; struct l{p a,b;double angle;}
L[MAXN],Q[MAXN];
	inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
	Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
	while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f; } inline P operator-(p a,p b) {return (P) {a.x-b.x,a.y-b.y};} inline double operator * (P a,p b) {return a.x*b.y-a.y*b.x;}
	line bool operator < (L a,l b) {if (Fabs (a.angle-b.angle) <eps) return (A.B-A.A) * (B.B-A.A) >0;
else return a.angle<b.angle; } inline P inter (L l1,l L2) {double k1= (l2.b-l1.a) * (l1.b-l1.a), k2= (l1.b-l1.a) * (l2.a-l1.a), t=k1/(k1+K2);
Return (P) {l2.b.x+ (l2.a.x-l2.b.x) *t,l2.b.y+ (L2.A.Y-L2.B.Y) *t};
	} inline bool Judge (L l1,l l2,l t) {P p=inter (L1,L2);
Return (T.B-T.A) * (P-T.A) <0;
	} inline void Pre () {P[0].x=p[1].x;p[0].y=inf;
	P[n+1].x=p[n].x;p[n+1].y=inf;
	F (I,0,n) l[++cnt]= (l) {p[i],p[i+1]};
	F (i,1,cnt) l[i].angle=atan2 (l[i].b.y-l[i].a.y,l[i].b.x-l[i].a.x);
Sort (l+1,l+cnt+1);
	} inline void HPi () {int top=0,bot=1;
	Tot=1;
		F (i,2,cnt) {if (Fabs (l[i].angle-l[i-1].angle) >=eps) tot++;
	L[tot]=l[i];
	} Cnt=tot;
	Q[++TOP]=L[1];Q[++TOP]=L[2];
		F (i,3,cnt) {while (Bot<top&&judge (q[top-1],q[top],l[i)) top--;
		while (Bot<top&&judge (Q[bot+1],q[bot],l[i])) bot++;
	Q[++top]=l[i];
	while (Bot<top&&judge (Q[top-1],q[top],q[bot])) top--;
	while (Bot<top&&judge (Q[bot+1],q[bot],q[top])) bot++;
	tot=0;
F (i,bot,top-1) a[++tot]=inter (q[i],q[i+1]);
		} inline void Getans () {f (K,1,tot) F (i,1,n-1) {P t= (p) {a[k].x,-1}; if (a[k].x>=p[i].x&&a[k].x<=p[i+1].x) Ans=min (Ans,a[k].y-inter ((l) {p[i],p[i+1]}, (l) {T,a[k]}). y);
		} f (K,1,n) F (i,1,tot-1) {P t= (p) {p[k].x,-1};
	if (p[k].x>=a[i].x&&p[k].x<=a[i+1].x) Ans=min (Ans,inter ((l) {a[i],a[i+1]}, (l) {T,p[k]}). y-p[k].y);
	int main () {n=read ()};
	F (I,1,n) p[i].x=read ();
	F (I,1,n) p[i].y=read ();
	Pre ();
	HPI ();
	Getans ();
	printf ("%.3lf\n", ans);
return 0;
 }


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.