hdu - 5033 - Building(单调栈)
题意:N 幢楼排成一列(1<=N<=10^5),各楼有横坐标 xi(1<=xi<=10^7) 以及高度 hi(1<=hi<=10^7),在各楼之间的Q个位置(1<=Q<=10^5),问这些位置能够仰望天空的夹角是多少度。
题目链接:http://acm.hdu.edu.cn/showproblem.php?
pid=5033
——>>将楼和人的位置一起按 x 排序。。
从左往右扫,单调栈维护斜率小的。
。
从右往左扫。单调栈维护斜率大的。。
#include <cstdio>
#include <algorithm>
#include <cmath> using std::sort; const double EPS = 1e-8;
const double PI = acos(-1.0);
const int MAXN = 100000 + 10; int N, Q, kase;
double L[MAXN], R[MAXN]; int Dcmp(double x)
{
if (fabs(x) < EPS) return 0;
return x > 0 ? 1 : -1;
} struct BUILD
{
double x;
double h;
int id; bool operator < (const BUILD& e) const
{
return Dcmp(x - e.x) < 0;
}
} b[MAXN << 1]; double Slope(const BUILD& a, const BUILD& b)
{
return (b.h - a.h) / (b.x - a.x);
} int st[MAXN];
struct MS
{
int top; MS(): top(0) {} void Push(int id)
{
st[top++] = id;
} void PopMax(const BUILD* b, const int& id)
{
while (top >= 2 && Dcmp(Slope(b[id], b[st[top - 1]]) - Slope(b[id], b[st[top - 2]])) >= 0)
{
--top;
}
} void PopMin(const BUILD* b, const int& id)
{
while (top >= 2 && Dcmp(Slope(b[id], b[st[top - 1]]) - Slope(b[id], b[st[top - 2]])) <= 0)
{
--top;
}
} int Top()
{
return st[top - 1];
}
}; void Read()
{
scanf("%d", &N);
for (int i = 1; i <= N; ++i)
{
scanf("%lf%lf", &b[i].x, &b[i].h);
b[i].id = 0;
}
scanf("%d", &Q);
for (int i = 1; i <= Q; ++i)
{
scanf("%lf", &b[i + N].x);
b[i + N].id = i;
b[i + N].h = 0.0;
}
} void Init()
{
sort(b + 1, b + 1 + N + Q);
} void GetLeft()
{
MS ms; for (int i = 1; i <= N + Q; ++i)
{
if (!b[i].id)
{
ms.PopMax(b, i);
ms.Push(i);
}
else
{
ms.PopMax(b, i);
int j = ms.Top();
L[b[i].id] = b[j].h / (b[i].x - b[j].x);
}
}
} void GetRight()
{
MS ms; for (int i = N + Q; i >= 1; --i)
{
if (!b[i].id)
{
ms.PopMin(b, i);
ms.Push(i);
}
else
{
ms.PopMin(b, i);
int j = ms.Top();
R[b[i].id] = b[j].h / (b[j].x - b[i].x);
}
}
} void Output()
{
printf("Case #%d:\n", ++kase);
for (int i = 1; i <= Q; ++i)
{
printf("%.10f\n", 180.0 / PI * (PI - atan(L[i]) - atan(R[i])));
}
} int main()
{
int T; kase = 0;
scanf("%d", &T);
while (T--)
{
Read();
Init();
GetLeft();
GetRight();
Output();
} return 0;
}
hdu - 5033 - Building(单调栈)的更多相关文章
- HDU 5033 Building(单调栈)
HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...
- hdu 5033 Building (单调栈 或 暴力枚举 )
Description Once upon a time Matt went to a small town. The town was so small and narrow that he can ...
- HDU - 5033 Building (单调栈+倍增)
题意:有一排建筑,每座建筑有一定的高度,宽度可以忽略,求在某点的平地上能看到天空的最大角度. 网上的做法基本都是离线的...其实这道题是可以在线做的. 对于向右能看到的最大角度,从右往左倍增维护每个时 ...
- HDU 5033 Building(单调栈维护凸包)
盗张图:来自http://blog.csdn.net/xuechelingxiao/article/details/39494433 题目大意:有一排建筑物坐落在一条直线上,每个建筑物都有一定的高度, ...
- HDU 5033 Building(北京网络赛B题) 单调栈 找规律
做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time L ...
- HDU 5033 Building (维护单调栈)
题目链接 题意:n个建筑物,Q条询问,问所在的位置,看到天空的角度是多少,每条询问的位置左右必定是有建筑物的. 思路 : 维护一个单调栈,将所有的建筑物和所有的人都放到一起开始算就行,每加入一个人,就 ...
- hdu 5033 模拟+单调优化
http://acm.hdu.edu.cn/showproblem.php?pid=5033 平面上有n个建筑,每个建筑由(xi,hi)表示,m组询问在某一个点能看到天空的视角范围大小. 维护一个凸包 ...
- Largest Rectangle in a Histogram HDU - 1506 (单调栈)
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...
- HDU5033 building 单调栈+计算几何
正解:单调栈 解题报告: 哇生气辽QAQ本来打了半天feel good都快调出来了然后说换题了QAQ(所以可能那题的代码会过一阵子再放上来了QAQ 不过还是大爆手速打了一通拿到首杀了嘻嘻 美滋滋辽 然 ...
随机推荐
- iOS8下的远程推送
本篇文章主要介绍了"iOS8下的远程推送",主要涉及到方面的内容,对于IOS开发感兴趣的同学可以参考一下: 昨天做了一下远程推送,今天写下来,分享给需要的人.参考了很多篇文章,或许 ...
- C语言基础之运算符优先级
1.运算符的优先级表 运算符的优先级(从高到低) 优先级 描述 运算符 1 括号 ().[] 2 正负号 +.- 3 自增自减,非 ++.--.! 4 乘除,取余 *./.% 5 加减 +.- 6 移 ...
- 用gulp+webpack构建多页应用——记一次Node多页应用的构建过程
通过参考网上的一些构建方法,当然也在开发过程中进行了一番实践,最终搭建了一套适用于当前多页应用的构建方案,当然该方案还处于draft版本,会在后续的演进过程中不断的优化. 个人觉得该方案的演进过程相对 ...
- UBIFS 术语
B+ tree: base head: budgeting: 空闲空间评估 bud: 一个日志使用的eraseblock cnode: commit: 更新index到flash上的过程 commit ...
- MVC中自定义ViewPage和WebViewPage
ViewPage和WebViewPage的作用就是将Controller中数据返回给页面,一个是针对aspx一个相对cshtml的.代码如下: public abstract class WebVie ...
- django book表单验证学习
django提供了强大的表单验证功能form 个人认为是把html代码封装到form里面,就像封装models中的数据一样.而且带有强大的验证功能,不需要后台再去写一堆乱七八糟的验证 具体操作查看dj ...
- ISP模块之色彩增强算法--HSV空间Saturation通道调整 .
色彩增强不同于彩色图像增强,图像增强的一般处理方式为直方图均衡化等,目的是为了增强图像局部以及整体对比度.而色彩增强的目的是为了使的原有的不饱和的色彩信息变得饱和.丰富起来.对应于Photoshop里 ...
- 【Zookeeper】Zookeeper部署笔记
Zookeeper部署笔记 .上传zk安装包 .解压 .配置(先在一台节点上配置) .1添加一个zoo.cfg配置文件 $ZOOKEEPER/conf mv zoo_sample.cfg zoo.cf ...
- 12. 改变JDK编译版本【从零开始学Spring Boot】
spring Boot在编译的时候,是有默认JDK版本的,如果我们期望使用我们要的JDK版本的话,那么要怎么配置呢? 这个只需要修改pom.xml文件的<build>-- <plug ...
- kali渗透综合靶机(一)--Lazysysadmin靶机
kali渗透综合靶机(一)--Lazysysadmin靶机 Lazysysadmin靶机百度云下载链接:https://pan.baidu.com/s/1pTg38wf3oWQlKNUaT-s7qQ提 ...