BZOJ 1038 瞭望塔
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,注意考虑实数误差带来的问题。
Source
半平面交。对于每条线段,所能看到其整条线段的点一定的在其所延长直线的上方,因此我们可以对所以直线求一次半平面交。
然后,最优解一定在线段端点处或半平面交所得多边形的顶点处。
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; #define eps (1e-6)
#define oo ((double)(1ll<<50))
#define maxn 310
int n,m,tot,cnt;
double ans = oo;
struct NODE
{
double x,y;
friend inline NODE operator + (const NODE &p,const NODE &q) { return (NODE) {p.x+q.x,p.y+q.y}; }
friend inline NODE operator - (const NODE &p,const NODE &q) { return (NODE) {p.x-q.x,p.y-q.y}; }
friend inline NODE operator * (const NODE &p,const double &q) { return (NODE) {p.x*q,p.y*q}; }
friend inline double operator /(const NODE &p,const NODE &q) { return p.x*q.y-p.y*q.x; }
inline double alpha() { return atan2(y,x); }
}mou[maxn],pol[maxn],pp[maxn];
struct LINE
{
NODE p,v; double slop;
inline void maintain() { slop = v.alpha(); }
friend inline bool operator <(const LINE &l1,const LINE &l2) { return l1.slop < l2.slop; }
}lines[maxn],qq[maxn];
struct SCAN
{
double x,y; int id; bool sign;
friend inline bool operator <(const SCAN &a,const SCAN &b)
{
if (a.x != b.x) return a.x < b.x;
else return a.sign < b.sign;
}
}bac[maxn]; inline bool ol(const LINE &l,const NODE &p) { return l.v/(p-l.p) > ; } inline NODE cp(const LINE &a,const LINE &b)
{
NODE u = a.p - b.p;
double t = (b.v/u)/(a.v/b.v);
return a.p+a.v*t;
} inline bool para(const LINE &a,const LINE &b)
{
return fabs(a.v/b.v) < eps;
} inline void ready()
{
for (int i = ;i < n;++i)
{
lines[++tot] = (LINE) {mou[i],(mou[i+]-mou[i])*1e-};
lines[tot].maintain();
}
lines[++tot] = (LINE) {(NODE) {-oo,},(NODE){,-0.001}};
lines[tot].maintain(); lines[++tot] = (LINE) {(NODE) {,oo},(NODE){-0.001,}};
lines[tot].maintain(); lines[++tot] = (LINE) {(NODE) {oo,},(NODE){,0.001}};
lines[tot].maintain(); lines[++tot] = (LINE) {(NODE) {,-oo},(NODE){0.001,}};
lines[tot].maintain();
} inline int half_plane_intersection()
{
sort(lines+,lines+tot+);
int head,tail;
qq[head = tail = ] = lines[];
for (int i = ;i <= tot;++i)
{
while (head < tail&&!ol(lines[i],pp[tail-])) --tail;
while (head < tail&&!ol(lines[i],pp[head])) ++head;
qq[++tail] = lines[i];
if (para(qq[tail],qq[tail-]))
{
tail--;
if (ol(qq[tail],lines[i].p)) qq[tail] = lines[i];
}
if (head < tail) pp[tail-] = cp(qq[tail],qq[tail-]);
}
while (head < tail && !ol(qq[head],pp[tail-])) --tail;
if (tail-head <= ) return ;
pp[tail] = cp(qq[tail],qq[head]);
for (int i = head;i <= tail;++i) pol[++m] = pp[i];
pol[] = pol[m];
return m;
} inline void work()
{
int all = ;
for (int i = ;i <= n;++i)
bac[++all] = (SCAN) { mou[i].x,mou[i].y,i,false };
for (int i = ;i <= m;++i)
if (pol[i].x >= mou[].x&&pol[i].x <= mou[n].x)
bac[++all] = (SCAN) { pol[i].x,pol[i].y,i,true };
sort(bac+,bac+all+);
int s1,s2;
for (int i = ;i <= all;++i) if (bac[i].sign) { s1 = bac[i].id-; break; }
for (int i = ;i <= all;++i)
{
LINE l = (LINE) {(NODE) {bac[i].x,},(NODE) {,}},l1; NODE p;
if (!bac[i].sign)
{
l1= (LINE) {pol[s1],pol[s1+]-pol[s1]};
s2 = bac[i].id;
}
else
{
l1= (LINE) {mou[s2],mou[s2+]-mou[s2]};
s1 = bac[i].id;
}
p = cp(l,l1);
ans = min(ans,fabs(p.y-bac[i].y));
}
} int main()
{
freopen("1038.in","r",stdin);
freopen("1038.out","w",stdout);
scanf("%d ",&n);
for (int i = ;i <= n;++i) scanf("%lf",&mou[i].x);
for (int i = ;i <= n;++i) scanf("%lf",&mou[i].y);
ready();
half_plane_intersection();
work();
printf("%.3lf",ans);
fclose(stdin); fclose(stdout);
return ;
}
BZOJ 1038 瞭望塔的更多相关文章
- bzoj 1038 瞭望塔 半平面交+分段函数
题目大意 给你一座山,山的形状在二维平面上为折线 给出\((x_1,y_1),(x_2,y_2)...(x_n,y_n)\)表示山的边界点或转折点 现在要在\([x_1,x_n]\)(闭区间)中选择一 ...
- 【BZOJ】【1038】【ZJOI2008】瞭望塔
计算几何/半平面交 说是半平面交,实际上只是维护了个下凸壳而已……同1007水平可见直线 对于每条线段,能看到这条线段的点都在这条线段的“上方”,那么对所有n-1条线段求一个可视区域的交,就是求一个半 ...
- 【BZOJ 1038】 1038: [ZJOI2008]瞭望塔
1038: [ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 ...
- 【BZOJ 1038】[ZJOI2008]瞭望塔
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1038 [题意] [题解] 可以看到所有村子的瞭望塔所在的位置只会是在相邻两个村子所代表 ...
- 1038: [ZJOI2008]瞭望塔 - BZOJ
Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, ...
- bzoj千题计划126:bzoj1038: [ZJOI2008]瞭望塔
http://www.lydsy.com/JudgeOnline/problem.php?id=1038 本题可以使用三分法 将点按横坐标排好序后 对于任意相邻两个点连成的线段,瞭望塔的高度 是单峰函 ...
- 1038: [ZJOI2008]瞭望塔
半平面交. 半平面指的就是一条直线的左面(也不知道对不对) 半平面交就是指很多半平面的公共部分. 这道题的解一定在各条直线的半平面交中. 而且瞭望塔只可能在各个点或者半平面交折线的拐点处. 求出半平面 ...
- 「BZOJ1038」「洛谷P2600」「ZJOI2008」瞭望塔 半平面交+贪心
题目链接 BZOJ/洛谷 题目描述 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安. 我们将H村抽象为一维的轮廓.如下图所示: 我们可以用一条山的上方 ...
- [BZOJ1038][ZJOI2008]瞭望塔(半平面交)
1038: [ZJOI2008]瞭望塔 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2999 Solved: 1227[Submit][Statu ...
随机推荐
- JS时钟钟表
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- java websockect
https://github.com/TooTallNate/Java-WebSocket (websockect类库包) http://blog.openlg.net/index.php/archi ...
- python模拟登陆之下载
好长时间没有更新博客了,哈哈. 今天公司给了这么一个需求,现在我们需要去淘宝获取上一天的订单号,然后再根据订单号去另一个接口去获取订单详情,然后再给我展示到web! 中间涉及到的技术点有: 模拟登陆 ...
- iOS-CGContextRef画各种图形例子
iOS-CGContextRef画各种图形例子 绘制效果图 绘制代码 - (void)drawRect:(CGRect)rect { //一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你 ...
- 你需要知道的 Android 拍照适配方案
近段时间,家里陪自己度过大学四年的电脑坏了,挑选好的新电脑配件终于在本周全部到货,自己动手完成组装.从AMD到i7的CPU,6G内存到14G内存,打开 AndroidStudio 的速度终于杠杆的上去 ...
- Change Fragment layout on orientation change
Warning: this may be a pre-Lollipop answer. A Fragment doesn't get re-inflated on configuration chan ...
- Bash关闭输出(关闭正确、错误输出)
利用&>重定向,不输出任何内容: echo hello &> /dev/null 关闭正确输出: echo hello 1> /dev/null 关闭错误输出: ec ...
- asp.net mvc 实现记忆返回的功能
大体思路是在当前跳转链接追加一个参数memoryguid,以guid为key把查询query保存在cookie里,跳转的时候带走这个guid,回来的时候还带着,这样我们就能根据这个guid从cooki ...
- HTML设置固定页脚飘浮
Css /* 页脚 */.footSty{bottom: 0pt; margin: 0pt; position: fixed; width: 100%; z-index: 10 ! important ...
- hadoop之wordCount程序理解
有篇文章http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html中介绍的