题意: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. spoj 913 Query on a tree II (倍增lca)

    Query on a tree II You are given a tree (an undirected acyclic connected graph) with N nodes, and ed ...

  2. (寒假集训)Cow Art(bfs)

    Cow Art 时间限制: 1 Sec  内存限制: 64 MB提交: 13  解决: 10[提交][状态][讨论版] 题目描述 A little known fact about cows is t ...

  3. NOIP2016_day1_No1玩具谜题

    #include <iostream> #include <cstdio> using namespace std; int main () { freopen("t ...

  4. luogu P1373 小a和uim之大逃离

    题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一道道闪电,一阵阵雷声.刹那间,狂风大作,乌云布满了天空,紧接着豆大的雨点从天空中打落下来,只见前方出现了一个 ...

  5. POJ1226 Substrings(二分+后缀数组)

    题意:给n个字符串,求最长的子串,满足它或它的逆置出现在所有的n个字符串中. 把n个字符串及其它们的逆置拼接,中间用不同字符隔开,并记录suffix(i)是属于哪个字符串的: 跑后缀数组计算heigh ...

  6. HDU 2586 How far away? LCA 转化成RMQ

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 [题意] 给出一个N 个和N-1条边的连通图,询问任意两点间的距离.N<=40000 . [分 ...

  7. Mysql insert without auto-increase when duplicate

    INSERT INTO video_tag_all(tagname,ctime) FROM video_tag_all WHERE (SELECT last_insert_id(id) FROM vi ...

  8. 50个最常用的UNIX/Linux命令

    转自http://get.jobdeer.com/493.get 1. tar command examples Create a new tar archive. $ tar cvf archive ...

  9. Android可伸缩列表ExpandableListView

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  10. Java:网络编程值TCP的使用

    演示TCP传输   1.Tcp分客户端和服务端 2.客服端对应的对象是scoket    服务端对应的对象是serverscoket   客户端: 通过查阅scoket对象,发现在建立对象时,就可以连 ...