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条询问,问所在的位置,看到天空的角度是多少,每条询问的位置左右必定是有建筑物的. 思路 : 维护一个单调栈,将所有的建筑物和所有的人都放到一起开始算就行,每加入一个人,就 ...
随机推荐
- (转)SSIS_数据流转换(Union All&合并联接&合并)
Union All : 与sql语言 Union All 一样,不用排序,上下合并多个表.Union All转换替代合并转换:输入输出无需排序,合并超过两个表 合并联接 : 有左连接.内连接.完全连接 ...
- devise 自定义手机号登录
user model中配置 validates_uniqueness_of :phone def email_required? false end 修改user migration文件,给phone ...
- Linux 安装配置 Nginx
前言 准备用flask做一个自己的博客网站,打算用Nginx来部署,所以在阿里云的服务器上安装Nginx,参考了很多教程,现在将步骤以及自己遇到的坑写下来,希望能对别人有所帮助. 我用的服务器是阿里云 ...
- 牛客国庆集训派对Day2 Solution
A 矩阵乘法 思路: 1° 牛客机器太快了,暴力能过. #include <bits/stdc++.h> using namespace std; #define N 5000 in ...
- ehcache实现页面整体缓存和页面局部缓存
之前写过spring cache和ehcache的基本介绍和注解实现缓存管理,今天记录下web项目的页面缓存技术. 页面缓存是否有必要?. 这样说吧,几乎所有的网站的首页都是访问率最高的,而首页上的数 ...
- SprigBoot核心技术
启动原理 运行流程 自动配置原理 一.启动原理 SpringApplication.run(主程序类)– new SpringApplication(主程序类)• 判断是否web应用• 加载并保存所有 ...
- autofac &web api 切换数据库
https://stackoverflow.com/questions/24188025/is-there-another-way-of-changing-database-instance-in-a ...
- Gym 101147A The game of Osho(SG找规律)
https://vjudge.net/problem/Gym-101147A 题意:给出G组数,每组数包括两个数B,N,两玩家轮流取数,使得N-num,num<=N并且num是N的整次幂.判断谁 ...
- Linux下的IPC几种通信方式
Linux下的IPC几种通信方式 管道(pipe):管道可用于具有亲缘关系的进程间的通信,是一种半双工的方式,数据只能单向流动,允许一个进程和另一个与它有公共祖先的进程之间进行通信. 命名管道(nam ...
- 使用 for 循环
for 循环通过迭代一个给定向量或列表,重复执行某个表达式.for 循环的语法是这样的:for (var in vector) {expr}var 遍历 vector 中的各个元素值,expr 被反复 ...