HDU5033 Building(单调栈)
题意是说在水平轴上有很多建筑物(没有宽度),知道每个建筑物的位置与高度。有m个查询,每次查询位置x所能看到的天空的角度。
方法是将建筑与查询一起排序,从左往右计算一遍,如果是建筑物,则比较最后两个(当前的与队尾的)斜率与队尾两个的斜率比较,如果较小则入队,否则一直出队尾元素直至满足条件(因为斜率为负数,斜率较小说明越堵)。
如果是查询,同样的比较这个位置与队尾的斜率同队尾两个元素的斜率比较,直至满足小于的关系结束,这时计算垂直方向左侧的夹角
最后从右往左计算右边,求和便是答案
#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, (L + R)>>1
#define rson k<<1|1, ((L + R)>>1) + 1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define dec(i, a, b) for(int i = a; i >= b; i --) template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = + ;
const int MAXM = ;
const double eps = 1e-;
LL MOD = ;
const double PI = 4.0 * atan(1.0); int T, N, Q;
int x, h;
struct Node {
int x, h;
Node(int _x = , int _h = -) {
x = _x; h = _h;
}
}node[MAXN];
int sta[MAXN >> ];
double ans_l[MAXN >> ], ans_r[MAXN >> ]; int cmp_up(Node A, Node B) { return A.x < B.x; } int cmp_down(Node A, Node B) { return A.x > B.x; } double pre(int r, int l) {
return (double)(node[r].h - node[l].h) / fabs(1.0 * node[r].x - node[l].x);
} double pre(Node a, int l) {
return (double)(a.h - node[l].h) / fabs(1.0 * a.x - node[l].x);
} void record_ans(double *angle) {
int tp = ;
rep (i, , N + Q) {
if(node[i].h < ) {
while(tp >= && pre(Node(node[i].x, ), sta[tp - ]) - pre(sta[tp - ], sta[tp - ]) > eps) tp --;
angle[-node[i].h] = atan(fabs(1.0 * node[i].x - node[sta[tp - ]].x) / node[sta[tp - ]].h);
}
else {
while(tp >= && pre(i, sta[tp - ]) - pre(sta[tp - ], sta[tp - ]) > eps) tp --;
sta[tp++] = i;
}
}
} int main()
{
// FIN;
cin >> T;
rep (cas, , T) {
scanf("%d", &N);
rep (i, , N) {
scanf("%d %d", &x, &h);
node[i] = Node(x, h);
}
scanf("%d", &Q);
rep (i, , Q) {
scanf("%d", &x);
node[N + i] = Node(x, -i);
}
sort(node + , node + + N + Q, cmp_up);
record_ans(ans_l);
sort(node + , node + + N + Q, cmp_down);
record_ans(ans_r);
cout << "Case #" << cas <<":" << endl;
rep (i, , Q) {
printf("%.10f\n", (ans_l[i] + ans_r[i]) * / PI);
}
}
return ;
}
HDU5033 Building(单调栈)的更多相关文章
- HDU5033 building 单调栈+计算几何
正解:单调栈 解题报告: 哇生气辽QAQ本来打了半天feel good都快调出来了然后说换题了QAQ(所以可能那题的代码会过一阵子再放上来了QAQ 不过还是大爆手速打了一通拿到首杀了嘻嘻 美滋滋辽 然 ...
- HDU 5033 Building(单调栈)
HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...
- hdu5033 Building 单调队列
// hdu5033 Building 单调队列 // // 题目大意: // // n栋大楼,有一个高度h和位置x.如今有一个人高度为0,有q个询问 // 每一个询问有一个位置x,求在位置x能看到天 ...
- hdu 5033 Building (单调栈 或 暴力枚举 )
Description Once upon a time Matt went to a small town. The town was so small and narrow that he can ...
- HDU - 5033 Building (单调栈+倍增)
题意:有一排建筑,每座建筑有一定的高度,宽度可以忽略,求在某点的平地上能看到天空的最大角度. 网上的做法基本都是离线的...其实这道题是可以在线做的. 对于向右能看到的最大角度,从右往左倍增维护每个时 ...
- HDU 5033 Building(单调栈维护凸包)
盗张图:来自http://blog.csdn.net/xuechelingxiao/article/details/39494433 题目大意:有一排建筑物坐落在一条直线上,每个建筑物都有一定的高度, ...
- hdu5033 Building (单调栈+)
http://acm.hdu.edu.cn/showproblem.php?pid=5033 2014 ACM/ICPC Asia Regional Beijing Online B 1002 Bui ...
- HDU 5033 Building(北京网络赛B题) 单调栈 找规律
做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time L ...
- HDU 5033 Building (维护单调栈)
题目链接 题意:n个建筑物,Q条询问,问所在的位置,看到天空的角度是多少,每条询问的位置左右必定是有建筑物的. 思路 : 维护一个单调栈,将所有的建筑物和所有的人都放到一起开始算就行,每加入一个人,就 ...
随机推荐
- Ubuntu 系统下暴力卸载 MySQL
一.概述 MySQL 出问题了,正常的 start.stop 不起作用. apt-get remove mysql-server apt-get remove mysql-client 上面这些命令不 ...
- Entity Framework Code First在Oracle下的伪实现(转)
为什么要说是伪实现,因为还做不到类似MsSql中那样完全的功能.Oralce中的数据库还是要我们自己手动去创建的.这里,我们舍掉了Model First中的EDMX文件,自己在代码里面写模型与映射关系 ...
- Java数据结构和算法总结-字符串相关高频面试题算法
前言:周末闲来无事,看了看字符串相关算法的讲解视频,收货颇丰,跟着视频讲解简单做了一下笔记,方便以后翻阅复习同时也很乐意分享给大家.什么字符串在算法中有多重要之类的大路边上的客套话就不多说了,直接上笔 ...
- Vim 操作命令不完全汇总
.命令:"重复上次修改": x命令:删除光标下的字符: u命令:撤销上次修改: dd命令:删除整行: >G命令:增加从当前行到文档末尾处的层级缩进: $命令:把光标移至行尾: ...
- 寻找List之和的最近素数
Task : Given a List [] of n integers , find minimum mumber to be inserted in a list, so that sum of ...
- 【Python初学者】准备
准备着手学习Python这门久仰大名的语言.本篇随笔是学习它的准备阶段. 操作系统: Mac OS 10.11.5 下载编辑器Testwrangler 点我 第一个Python程序 在命令行中运行py ...
- R语言数据去重
R语言常用的去重命令有unique duplicated unique主要是返回一个把重复元素或行给删除的向量.数据框或数组 > x <- c(3:5, 11:8, 8 + 0:5)> ...
- UOJ #164 【清华集训2015】 V
题目链接:V 这道题由于是单点询问,所以异常好写. 注意到每种修改操作都可以用一个标记\((a,b)\)表示.标记\((a,b)\)的意义就是\(x= \max\{x+a,b\}\) 同时这种标记也是 ...
- python 列表元素的筛选
color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow'] color = [x ,,)] print(color)
- [原][译][osgearth][EarthFile]关于EarthFile 的Model Layer 讲解(通过earth文件加载模型层)(OE官方文档翻译)
原文参考:http://docs.osgearth.org/en/latest/references/earthfile.html#model-layer 本人翻译能有限.... 模型层 模型层渲染“ ...