hdu 5033 单调栈 ****

看出来是单调栈维护斜率,但是不会写,2333,原来是和询问放在一起的

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <queue>
 6 #include <cmath>
 7 typedef __int64 ll;
 8 using namespace std;
 9
10 const double PI = acos(-1.0);
11 const int maxn = 100005;
12
13 struct Node {
14     int x, h;
15     bool operator <(const Node &a)const {
16         return x < a.x;
17     }
18 } node[maxn<<2], stk[maxn<<2];
19 double ans[maxn];
20 int n, q;
21
22 int check(Node &a, Node &b, Node c) {
23     if (c.h <= 0)
24         c.h = 0;
25     return (ll)(a.h - c.h) * (c.x - b.x) >= (ll)(b.h - c.h) * (c.x - a.x);
26 }
27
28 double getAngle(Node a, Node b) {
29     return atan((double)(b.x-a.x)/(double)(a.h));
30 }
31
32 void cal() {
33     int head = 0;
34     for (int i = 0; i < n+q; i++) {
35         if (node[i].h <= 0) {
36             while (head >= 2 && check(stk[head-2], stk[head-1], node[i]))
37                 head--;
38             ans[-node[i].h] += getAngle(stk[head-1], node[i]);
39         }
40         else {
41             while (head && stk[head-1].h <= node[i].h)
42                 head--;
43             while (head >= 2 && check(stk[head-2], stk[head-1], node[i]))
44                 head--;
45             stk[head++] = node[i];
46         }
47     }
48 }
49
50 int main() {
51     int t, cas = 1;
52     scanf("%d", &t);
53     while (t--) {
54         scanf("%d", &n);
55         for (int i = 0; i < n; i++)
56             scanf("%d%d", &node[i].x, &node[i].h);
57         scanf("%d", &q);
58         for (int i = 0; i < q; i++) {
59             scanf("%d", &node[i+n].x);
60             node[i+n].h = -i;
61         }
62
63         memset(ans, 0, sizeof(ans));
64         sort(node, node+n+q);
65
66         cal();
67
68         reverse(node, node+n+q);
69         for (int i = 0; i < n+q; i++)
70             node[i].x = 10000000 - node[i].x;
71
72         cal();
73
74         printf("Case #%d:\n", cas++);
75         for (int i = 0; i < q; i++)
76             printf("%.10lf\n", ans[i] * 180.0 / PI);
77     }
78     return 0;
79 }
时间: 2024-11-11 00:13:57

hdu 5033 单调栈 ****的相关文章

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

题意: 一个人在x轴上,他的左右两侧都有高楼,给出楼的横坐标Xi和高度Hi还有人的位置pos,求人所能看到的天空的最大角度. 分析: 将建筑物和人的位置从左到右排序,对于每个位置利用栈求一次人左边建筑物的凸包,找到一个最小的角度,然后对称一下,再找一个右边的建筑物的最小角度,两个角度加起来就是答案. 将人左边的建筑物从左到右扫描,下面两种情况会出栈: 栈顶元素楼高小于等于当前扫描到的楼高,因此这是一个单调的栈 栈顶两个楼顶所在直线的斜率 小于 栈顶的楼顶和当前楼顶所在直线的斜率(这里的斜率指的是

hdu 1506 单调栈问题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目的意思其实就是要找到一个尽可能大的矩形来完全覆盖这个矩形下的所有柱子,只能覆盖柱子,不能留空. 我们求得的面积其实就是Max{s=(right[i] - left[i] + 1)*height[i];(i>=1&&i<=n)} 每一个柱子都要尽可能向左向右延伸,使得获得最大的面积. 此时我就要用到单调栈 单调栈就是栈内元素单调递增或者单调递减的栈,单调栈只能在栈顶操作.

HDU 5033---Building(单调栈)

题目链接 Problem Description Once upon a time Matt went to a small town. The town was so small and narrow that he can regard the town as a pivot. There were some skyscrapers in the town, each located at position xi with its height hi. All skyscrapers loc

hdu 5875(单调栈)

Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1866    Accepted Submission(s): 674 Problem Description The shorter, the simpler. With this problem, you should be convinced of this tr

hdu 3410 单调栈

http://acm.hdu.edu.cn/showproblem.php?pid=3410 Passing the Message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 827    Accepted Submission(s): 546 Problem Description What a sunny day! Let's

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 排序.. 从左往右扫,单调栈维护斜率小的.. 从右往左扫,单调栈维护斜率大的.. #inc

hdu 5033 buiding(单调栈)

hdu 5033 buiding(单调栈) 某年某月某天,马特去了一个小镇.这个小镇如此狭窄,以至于他可以把小镇当作一个枢纽.在镇上有一些摩天大楼,其中一栋位于xi,高度为hi.所有的摩天大楼位于不同的地方.为了简化题目,假设摩天大楼没有宽度.由于摩天大楼如此之高,马特几乎看不到天空.对于马特所在的位置,他想知道他能看到天空的角度范围有多大.假设马特的身高是0.可以保证,对于每个查询,马特的左右两边至少有一座建筑物,而且他的位置上没有建筑物.建筑物的数量n<1e5,查询次数Q<1e5. 我用常

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

做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1257    Accepted Submission(s): 358 Special Judg

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

Description Once upon a time Matt went to a small town. The town was so small and narrow that he can regard the town as a pivot. There were some skyscrapers in the town, each located at position x i with its height h i. All skyscrapers located in dif