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 ;
}

bzoj1038: [ZJOI2008]瞭望塔的更多相关文章

  1. bzoj千题计划126:bzoj1038: [ZJOI2008]瞭望塔

    http://www.lydsy.com/JudgeOnline/problem.php?id=1038 本题可以使用三分法 将点按横坐标排好序后 对于任意相邻两个点连成的线段,瞭望塔的高度 是单峰函 ...

  2. [BZOJ1038][ZJOI2008]瞭望塔(半平面交)

    1038: [ZJOI2008]瞭望塔 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2999  Solved: 1227[Submit][Statu ...

  3. [日常摸鱼]bzoj1038 [ZJOI2008]瞭望塔-模拟退火/几何

    题意:给一条平面内$n$个点的折线,要求在折线上搞一个高度$h$的瞭望塔,能够看见折线上所有的点,求$h$的最小值($n \leq 300$) updata2018.1.21 正解半平面交在另一篇里面 ...

  4. 【半平面交】bzoj1038 [ZJOI2008]瞭望塔

    http://m.blog.csdn.net/blog/qpswwww/44105605 #include<cstdio> #include<cmath> #include&l ...

  5. BZOJ-1038 [ZJOI2008]瞭望塔

    先求半平面交,然后建塔的地方肯定是在半平面交的交点上或者是在地面线段的交点上. #include <cstdlib> #include <cstdio> #include &l ...

  6. [日常摸鱼]bzoj1038[ZJOI2008]瞭望塔-半平面交

    这回好好用半平面交写一次- 看了cls当年写的代码看了好久大概看懂了-cls太强辣 #include<cstdio> #include<iostream> #include&l ...

  7. 【BZOJ1038】[ZJOI2008]瞭望塔 半平面交

    [BZOJ1038][ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如 ...

  8. 【bzoj1038】瞭望塔

    [bzoj1038]瞭望塔 题意 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折 ...

  9. 【BZOJ 1038】 1038: [ZJOI2008]瞭望塔

    1038: [ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 ...

随机推荐

  1. Abator自动生成ibatis的相关配置和类

      一.abator自动生成配置: abator自动生成ibatis的配置文件.mode.sqlMap.dao.bean实例 准备: 1.安装Eclipse插件:      要求eclipse3.1, ...

  2. Parallel.Foreach的并发问题解决方法-比如爬虫WebClient

    场景五:线程局部变量 Parallel.ForEach 提供了一个线程局部变量的重载,定义如下: public static ParallelLoopResult ForEach<TSource ...

  3. python 分支 循环

    一  python 知识点      1.变量(标签):           a(变量名) = '字符串'or 数字 (整形和浮点型)     a相当于变量名的标签           如: a = ...

  4. iOS开发总结-类似京东分类,UICollectionView

    // // TypeViewController.m // BJ // // Created by shirenfeng on 16/11/6. // Copyright © 2016年 com.ws ...

  5. CHAR 详解

    CHAR(20):20指的是表中的a字段能存储的最大字符个数 CREATE TABLE `a` ( `a` char(20) DEFAULT NULL) ENGINE=InnoDB DEFAULT C ...

  6. media query

    accepted Another useful media feature is device-aspect-ratio. Note that the iPhone 5 does not have a ...

  7. 编程基础-msdn编程指南笔记

    此博仅为笔记,摘自msdn编程指南文档,链接地址:http://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx 注释:// 单行注释 /* 多行注释*/ ...

  8. CAEmitterLayer

    -(void)createFireworks{ CAEmitterLayer *fireworks = [CAEmitterLayer layer]; fireworks.emitterPositio ...

  9. XCODE6 提交至 App Store

    新到一个公司,以前的苹果开发人员离职,临时接手他的苹果代码,需要修改并上线到APP STORE.  xcode6.0升级到最新的6.1后, 发现各种坑 1. 路径配置不对, 这个是个人习惯问题,之前的 ...

  10. UILabel的高度自适应

    _content = [UILabel new]; _content.text = @"日落时分,沏上一杯山茶,听一曲意境空远的<禅>,心神随此天籁,沉溺于玄妙的幻境里.仿佛我就 ...