Description

致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安。我们将H村抽象为一维的轮廓。如下图所示 我们可以用一条山的上方轮廓折线(x1, y1), (x2, y2), …. (xn, yn)来描述H村的形状,这里x1 < x2 < …< xn。瞭望塔可以建造在[x1, xn]间的任意位置, 但必须满足从瞭望塔的顶端可以看到H村的任意位置。可见在不同的位置建造瞭望塔,所需要建造的高度是不同的。为了节省开支,dadzhi村长希望建造的塔高度尽可能小。请你写一个程序,帮助dadzhi村长计算塔的最小高度。

Input

第一行包含一个整数n,表示轮廓折线的节点数目。接下来第一行n个整数, 为x1 ~ xn. 第三行n个整数,为y1 ~ yn。

Output

仅包含一个实数,为塔的最小高度,精确到小数点后三位。

Sample Input

【输入样例一】
6
1 2 4 5 6 7
1 2 2 4 2 1
【输入样例二】
4
10 20 49 59
0 10 10 0

Sample Output

【输出样例一】
1.000
【输出样例二】
14.500

HINT

对于100%的数据, N ≤ 300,输入坐标绝对值不超过106,注意考虑实数误差带来的问题。

 
题解:
http://blog.csdn.net/popoqqq/article/details/39340759
code:
 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char ch;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
void read(double &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
const int maxn=;
const double eps=1E-;
int n;
double x[maxn],y[maxn],l,m,r,lim;
double getrand(){return rand()%/1000000.0;}
double cross(double x,double y,double xx,double yy){return x*yy-xx*y;}
bool check(double pos,double h){
for (int i=;i<=n;i++) if (cross(x[i-]-pos,y[i-]-h,x[i]-pos,y[i]-h)<) return false;
return true;
}
double get_h(double pos){
for (int i=;i<=n;i++) if (x[i-]<=pos&&pos<=x[i]) return y[i-]+(y[i]-y[i-])/(x[i]-x[i-])*(pos-x[i-]);
return -1E12;
}
double calc(double pos){
l=get_h(pos),r=1E12;
while (r-l>eps){
m=(l+r)/;
if (check(pos,m)) r=m; else l=m;
}
return l-get_h(pos);
}
void sa(){
double now=x[]+getrand()*lim,nxt,t1=calc(now),t2;
for (double T=lim;T>0.000001;T*=0.996){
double dx=T*(getrand()*-);
nxt=now+dx;
t2=calc(nxt);
if (exp((t1-t2)/T)>getrand()) now=nxt,t1=t2;
}
printf("%.3lf\n",t1);
}
int main(){
srand();
read(n);
for (int i=;i<=n;i++) read(x[i]);
for (int i=;i<=n;i++) read(y[i]);
lim=x[n]-x[],sa();
return ;
}
05-04 09:17