题意: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(单调栈)的更多相关文章

  1. HDU 5033 Building(单调栈)

    HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...

  2. hdu 5033 Building (单调栈 或 暴力枚举 )

    Description Once upon a time Matt went to a small town. The town was so small and narrow that he can ...

  3. HDU - 5033 Building (单调栈+倍增)

    题意:有一排建筑,每座建筑有一定的高度,宽度可以忽略,求在某点的平地上能看到天空的最大角度. 网上的做法基本都是离线的...其实这道题是可以在线做的. 对于向右能看到的最大角度,从右往左倍增维护每个时 ...

  4. HDU 5033 Building(单调栈维护凸包)

    盗张图:来自http://blog.csdn.net/xuechelingxiao/article/details/39494433 题目大意:有一排建筑物坐落在一条直线上,每个建筑物都有一定的高度, ...

  5. HDU 5033 Building(北京网络赛B题) 单调栈 找规律

    做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time L ...

  6. HDU 5033 Building (维护单调栈)

    题目链接 题意:n个建筑物,Q条询问,问所在的位置,看到天空的角度是多少,每条询问的位置左右必定是有建筑物的. 思路 : 维护一个单调栈,将所有的建筑物和所有的人都放到一起开始算就行,每加入一个人,就 ...

  7. hdu 5033 模拟+单调优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5033 平面上有n个建筑,每个建筑由(xi,hi)表示,m组询问在某一个点能看到天空的视角范围大小. 维护一个凸包 ...

  8. 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 ...

  9. HDU5033 building 单调栈+计算几何

    正解:单调栈 解题报告: 哇生气辽QAQ本来打了半天feel good都快调出来了然后说换题了QAQ(所以可能那题的代码会过一阵子再放上来了QAQ 不过还是大爆手速打了一通拿到首杀了嘻嘻 美滋滋辽 然 ...

随机推荐

  1. Xamarin XAML语言教程Progress属性设置进度条进度

    Xamarin XAML语言教程Progress属性设置进度条进度 在图12.19~12.21中我们看到的是没有实现加载的进度条,即进度条的当前进度为0,如果开发者想要修改当前进度,可以使用两种方式: ...

  2. 集合框架(Collection和Collections的区别)

    1.Collection: java.util.Collection 是一个集合接口. 它提供了对集合对象进行基本操作的通用接口方法.Collection接口在Java 类库中有很多具体的实现.Col ...

  3. Bluetooth篇 开发实例之九 和蓝牙模块通信

    首先,我们要去连接蓝牙模块,那么,我们只要写客户端的程序就好了,蓝牙模块就相当于服务端. 连接就需要UUID. #蓝牙串口服务SerialPortServiceClass_UUID = ‘{00001 ...

  4. JAVA常见算法题(二十三)

    package com.xiaowu.demo; /** * 给一个不多于5位的正整数,要求:①求它是几位数:②逆序打印出各位数字. * * * @author WQ * */ public clas ...

  5. [置顶] docker--基础镜像和dockerfile

    制作基础镜像 注意:需要在CentOS6下操作 准备工作 yum -y install febootstrap 下载ISO镜像文件到服务器 mkdir /mnt/centos6/ mount -o l ...

  6. android下socket编程问题:服务器关闭时,客户端发送请求的异常处理

    我用socket分别创建了一个服务器和一个客户端. 当服务器程序运行时,客户端和服务器发送接收数据是OK的. 但是,如果服务器程序关闭以后,客户端仍然发送请求的话,会抛出一个IOException.但 ...

  7. Console-算法:fun1(do while)

    ylbtech-Arithmetic:Console-算法[do while]-XX   1.A,Demo(案例) 1.B,Solution(解决方案) using System; namespace ...

  8. django book多站点学习

    多个站点 Django 的多站点系统是一种通用框架,它让你可以在同一个数据库和同一个Django项目下操作多个网站. 这是一个抽象概念,理解起来可能有点困难,因此我们从几个让它能派上用场的实际情景入手 ...

  9. 批处理学习:for语句详解

    大纲 一 前言 二 for语句的基本用法 三 for /f (delims.tokens.skip.eol.userbackq.变量延迟) 四 for /r (递归遍历) 五 for /d (遍历目录 ...

  10. linux命令之------vmstat使用

    在linux命令中,vmstat是个经常用到的分析系统性能的命令之一,主要有两个参数:一个是采样频率,一个是采样的次数.如:vmstat   1  3,意思就是每隔1秒采样1次,总共采样3次. 统计次 ...