题意: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;
}

最新文章

  1. CentOS 下使用yum安装nodejs
  2. android、IOS和手机基础知识
  3. DBA-mysql-字符集
  4. Password Attacker
  5. Oracle的控制文件
  6. tryparse的用法,^0*[1-9]\d*$
  7. WeCenter 社交化问答社区程序 | WeCenter 是一款知识型的社交化问答社区程序,专注于社区内容的整理、归类、检索和再发行
  8. JavaFX游戏开发效率浅谈
  9. could not perform addBatch
  10. java —— equals 与 ==
  11. 微信小程序开发入门篇
  12. SQL Server AlwaysOn 集群 关于主Server IP与Listener IP调换的详细测试
  13. WIN下的Django安装
  14. Object constructor
  15. C#正则表达式。
  16. LabVIEW(十二):VI本地化-控件标题内容的修改
  17. The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online -C:Halting Problem(模拟)
  18. linux下如何删除十字符libudev.so病毒文件
  19. Android Studio3.0 配置AndroidAnnotation注解框架
  20. android素材资源

热门文章

  1. 洛谷——P1143 进制转换
  2. Binary Tree Vertical Order Traversal -- LeetCode
  3. [xsy2724]Tree
  4. 所有iOS设备的屏幕分辨率
  5. 4.NFC前台调度系统
  6. C++ development cross platforms
  7. SQL数据库学习系列之一
  8. INNO SETUP脚本向导创建的基本脚本
  9. 【mybatis】mybatis中的&lt;if test=“”&gt;test中多条件
  10. [置顶] kubernetes资源类型--pod和job