HDU5033 Building(单调栈)

题意是说在水平轴上有很多建筑物(没有宽度),知道每个建筑物的位置与高度。有m个查询,每次查询位置x所能看到的天空的角度。

方法是将建筑与查询一起排序,从左往右计算一遍,如果是建筑物,则比较最后两个(当前的与队尾的)斜率与队尾两个的斜率比较,如果较小则入队,否则一直出队尾元素直至满足条件(因为斜率为负数,斜率较小说明越堵)。

如果是查询,同样的比较这个位置与队尾的斜率同队尾两个元素的斜率比较,直至满足小于的关系结束,这时计算垂直方向左侧的夹角

最后从右往左计算右边,求和便是答案

  1 #pragma comment(linker, "/STACK:1677721600")
  2 #include <map>
  3 #include <set>
  4 #include <stack>
  5 #include <queue>
  6 #include <cmath>
  7 #include <ctime>
  8 #include <vector>
  9 #include <cstdio>
 10 #include <cctype>
 11 #include <cstring>
 12 #include <cstdlib>
 13 #include <iostream>
 14 #include <algorithm>
 15 using namespace std;
 16 #define INF 0x3f3f3f3f
 17 #define inf (-((LL)1<<40))
 18 #define lson k<<1, L, (L + R)>>1
 19 #define rson k<<1|1,  ((L + R)>>1) + 1, R
 20 #define mem0(a) memset(a,0,sizeof(a))
 21 #define mem1(a) memset(a,-1,sizeof(a))
 22 #define mem(a, b) memset(a, b, sizeof(a))
 23 #define FIN freopen("in.txt", "r", stdin)
 24 #define FOUT freopen("out.txt", "w", stdout)
 25 #define rep(i, a, b) for(int i = a; i <= b; i ++)
 26 #define dec(i, a, b) for(int i = a; i >= b; i --)
 27
 28 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
 29 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
 30 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
 31 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
 32
 33 //typedef __int64 LL;
 34 typedef long long LL;
 35 const int MAXN = 200000 + 100;
 36 const int MAXM = 110000;
 37 const double eps = 1e-8;
 38 LL MOD = 1000000007;
 39 const double PI = 4.0 * atan(1.0);
 40
 41 int T, N, Q;
 42 int x, h;
 43 struct Node {
 44     int x, h;
 45     Node(int _x = 0, int _h = -1) {
 46         x = _x; h = _h;
 47     }
 48 }node[MAXN];
 49 int sta[MAXN >> 1];
 50 double ans_l[MAXN >> 1], ans_r[MAXN >> 1];
 51
 52 int cmp_up(Node A, Node B) { return A.x < B.x; }
 53
 54 int cmp_down(Node A, Node B) { return A.x > B.x; }
 55
 56 double pre(int r, int l) {
 57     return (double)(node[r].h - node[l].h) / fabs(1.0 * node[r].x - node[l].x);
 58 }
 59
 60 double pre(Node a, int l) {
 61     return (double)(a.h - node[l].h) / fabs(1.0 * a.x - node[l].x);
 62 }
 63
 64 void record_ans(double *angle) {
 65     int tp = 0;
 66     rep (i, 1, N + Q) {
 67         if(node[i].h < 0) {
 68             while(tp >= 2 && pre(Node(node[i].x, 0), sta[tp - 1]) - pre(sta[tp - 1], sta[tp - 2]) > eps) tp --;
 69             angle[-node[i].h] = atan(fabs(1.0 * node[i].x - node[sta[tp - 1]].x) / node[sta[tp - 1]].h);
 70         }
 71         else {
 72             while(tp >= 2 && pre(i, sta[tp - 1]) - pre(sta[tp - 1], sta[tp - 2]) > eps) tp --;
 73             sta[tp++] = i;
 74         }
 75     }
 76 }
 77
 78 int main()
 79 {
 80 //    FIN;
 81     cin >> T;
 82     rep (cas, 1, T) {
 83         scanf("%d", &N);
 84         rep (i, 1, N) {
 85             scanf("%d %d", &x, &h);
 86             node[i] = Node(x, h);
 87         }
 88         scanf("%d", &Q);
 89         rep (i, 1, Q) {
 90             scanf("%d", &x);
 91             node[N + i] = Node(x, -i);
 92         }
 93         sort(node + 1, node + 1 + N + Q, cmp_up);
 94         record_ans(ans_l);
 95         sort(node + 1, node + 1 + N + Q, cmp_down);
 96         record_ans(ans_r);
 97         cout << "Case #" << cas <<":" << endl;
 98         rep (i, 1, Q) {
 99             printf("%.10f\n", (ans_l[i] + ans_r[i]) * 180 / PI);
100         }
101     }
102     return 0;
103 }
时间: 2024-08-06 20:07:35

HDU5033 Building(单调栈)的相关文章

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

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

题意:有一排建筑,每座建筑有一定的高度,宽度可以忽略,求在某点的平地上能看到天空的最大角度. 网上的做法基本都是离线的...其实这道题是可以在线做的. 对于向右能看到的最大角度,从右往左倍增维护每个时刻的单调栈(凸壳),对于每个询问,先二分找到它右面的第一个建筑的位置,然后在对应的凸壳上倍增找到切点即可. 向左看把x坐标对称一下就行. 复杂度$O(nlogn)$ 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long lo

hdu5033 Building (单调栈+)

http://acm.hdu.edu.cn/showproblem.php?pid=5033 2014 ACM/ICPC Asia Regional Beijing Online B 1002 Building Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special

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(2014北京网络赛 单调栈+几何)

博客原文地址:http://blog.csdn.net/xuechelingxiao/article/details/39494433 Building 题目大意:有一排建筑物坐落在一条直线上,每个建筑物都有一定的高度,给出一个X坐标,高度为0,问X位置能看到的视角是多少度.如图: 图一: 图二: 图一为样例一,图二为样例三,红色部分为高楼,蓝色虚线为视角的最大范围. 解题思路: 由于有10w个点跟10w次询问,所以朴素的算法判断肯定会超时的.所以就需要用单调栈,维护相邻两建筑顶(xi,hi)的

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 Building (维护单调栈)

题目链接 题意:n个建筑物,Q条询问,问所在的位置,看到天空的角度是多少,每条询问的位置左右必定是有建筑物的. 思路 : 维护一个单调栈,将所有的建筑物和所有的人都放到一起开始算就行,每加入一个人,就维护栈里的建筑物的高度,也就是说这个人所能够看到的建筑物时在栈里的,但是这个人看不到的就删掉,例如下图,中间三个点可以删掉了,因为角度问题中间三个是看不到的,所以不会影响最终求得角度 还有一种情况,就是下述的情况,不停的维护单调栈,人站在不同的地方角度也是不一样的. 维护的是相邻两建筑顶(xi,hi

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

HDU4252 ACM fighting(单调栈)

Description After Mr. B arrived in Warsaw, he was shocked by the skyscrapers and took several photos. But now when he looks at these photos, he finds in surprise that he isn't able to point out even the number of buildings in it. So he decides to wor