瞭望塔

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

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

  4
  10 20 49 59
  0 10 10 0

Sample Output

  14.500

HINT

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

Solution

  首先,如果我们确定了一个点的话,显然是可以Check的。

  对于 每一个点连向这个点连线 必须是要逆时针方向的。

  那么如果有一个横坐标了,我们就可以二分答案了。怎么确定这个横坐标呢?

  乍一看,数据这么小:当然是模拟退火啦!上一波退火美滋滋。٩(๑>◡<๑)۶

Code

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
using namespace std;
typedef unsigned long long s64; const int ONE = ;
const double eps = 1e-; int n;
double from, to;
double Ans = 1e20; struct power
{
double x, y;
}a[ONE]; int get()
{
int res,Q=; char c;
while( (c=getchar())< || c>)
if(c=='-')Q=-;
if(Q) res=c-;
while((c=getchar())>= && c<=)
res=res*+c-;
return res*Q;
} int PD(double a, double b)
{
if(fabs(a - b) <= eps) return ;
if(a > b) return ;
return -;
} double Gety(double x)
{
for(int i = ; i <= n; i++)
if(PD(a[i-].x, x) <= && PD(x, a[i].x) <= )
{
double k = (a[i].y - a[i-].y) / (a[i].x - a[i-].x);
double b = a[i-].y;
return k * (x - a[i-].x) + b;
}
} double Cross(power a, power b, power c) {return (a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y);}
int Check(power A)
{
for(int i = ; i <= n; i++)
if(PD(Cross(a[i-], a[i], A), ) < ) return ;
return ;
} double Judge(double x)
{
double l = , r = 1e10, res;
double y = Gety(x);
while(l < r - 0.0001)
{
double mid = (l + r) / ;
if(Check( (power){x, y + mid} )) r = mid;
else l = mid;
}
if(Check( (power){x, y + l} )) res = l; else res = r;
Ans = min(Ans, res);
return res;
} double Random() {return (rand()%) / 1000.00;}
void SA(double T)
{
double Now = (from + to) / , A;
Judge(Now);
while(T >= 0.0001)
{
A = Now + T * (Random() * - );
if(!(from <= A && A <= to)) continue;
double dE = Judge(Now) - Judge(A);
if(dE > || Random() <= exp(dE / T))
Now = A;
T *= 0.993;
} for(double i = -; i <= ; i += 0.001)
Judge(Now + i);
} int main()
{
n = get();
for(int i = ; i <= n; i++) scanf("%lf", &a[i].x);
for(int i = ; i <= n; i++) scanf("%lf", &a[i].y); from = a[].x; to = a[n].x;
SA(to - from); printf("%.3lf", Ans);
}

【BZOJ1038】【ZJOI2008】瞭望塔 [模拟退火]的更多相关文章

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

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

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

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

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

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

  4. bzoj1038: [ZJOI2008]瞭望塔

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

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

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

  6. BZOJ-1038 [ZJOI2008]瞭望塔

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

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

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

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

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

  9. 【bzoj1038】瞭望塔

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

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

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

随机推荐

  1. 如果jsp表单元素的值为空,如何避免null出现在页面上?

    可以写一个简单的函数对空值进行处理,判断值是否为空,如果是空就返回空字符串.实例代码如下: <%! String blanknull(String s) { return (s == null) ...

  2. UVA725 Division (暴力求解法入门)

    uva 725 Division Write a program that finds and displays all pairs of 5-digit numbers that between t ...

  3. java键盘IO

    public class IO { public static void main(String[] args) throws Throwable { ScannerTest(); // testSc ...

  4. Ubuntu 12.04.1 LTS 升级 PHP 从5.3 到 5.5

    #!/bin/bash # desc install php5.5 #add-apt-repository ppa:ondrej/php5 #apt-get install python-softwa ...

  5. vi/sed等遵循的搜索正则语法

    转自:http://blog.csdn.net/lanxinju/article/details/5731843 一.查找 查找命令 /pattern<Enter> :向下查找patter ...

  6. python爬虫:爬取网站视频

    python爬取百思不得姐网站视频:http://www.budejie.com/video/ 新建一个py文件,代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 ...

  7. vs code 自动补全效果不理想的问题

    之前一直用webstorm,最近换换口味,改用了VS Code,发现VS Code 智能提示得到的都不是我想要的 就比如  ! + tab ,HTML结构都出不来.经过一番搜索,发现是 VS Code ...

  8. imshow(A,[])和imshow(A)的区别

    imshow的用法: IMSHOW Display image. IMSHOW(I,N) displays the intensity image I with N discrete levels o ...

  9. Django 2.0 学习(09):Django 静态文件(样式和背景图片)

    应用的定制化:静态文件 首先,在polls目录中创建一个名叫static的目录.Django会在该目录里面查找静态文件,类似于Django在polls/template目录下查找模板文件. Djang ...

  10. XML-RPC协议学习

    XML-RPC调用包括2部分:客户端client(调用线程).服务器端server(被调用的线程).服务端是通过特定的URL获得的,调用过程如下: 1.客户端程序使用XML-RPC客户端发出作业请求, ...