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 different place. The skyscrapers had no width, to make it simple. As the skyscrapers were so high, Matt could hardly see the sky.Given the position Matt was at, he wanted to know how large the angle range was where he could see the sky. Assume that Matt's height is 0. It's guaranteed that for each query, there is at least one building on both Matt's left and right, and no building locate at his position.
 

Input

The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow. 

Each test case begins with a number N(<=N<=^), the number of buildings. 

In the following N lines, each line contains two numbers, x i(<=x i<=^) and h i(<=h i<=^). 

After that, there's a number Q(1<=Q<=10^5) for the number of queries. 

In the following Q lines, each line contains one number q i, which is the position Matt was at.
 

Output

For each test case, first output one line "Case #x:", where x is the case number (starting from ). 

Then for each query, you should output the angle range Matt could see the sky in degrees. The relative error of the answer should be no more than ^(-).
 

Sample Input


 

Sample Output

Case #:
101.3099324740
Case #:
90.0000000000
Case #:
78.6900675260

第一种方法是用单调栈维护

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 200006
#define PI acos(-1.0)
int n,m;
struct Node{
double x,h;
int id;
double angle1;
double angle2;
bool vis;
}a[N],q[N];
bool cmp1(Node a,Node b){
return a.x<b.x;
}
bool cmp2(Node a,Node b){
return a.id<b.id;
}
double xieLv(Node a,Node b){
double w1=fabs(b.x-a.x);
double w2=b.h-a.h; return w2/w1;
}
int main()
{
int ac=;
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%lf%lf",&a[i].x,&a[i].h);
a[i].id=i;
a[i].vis=false;
}
scanf("%d",&m);
for(int i=n;i<n+m;i++){
scanf("%lf",&a[i].x);
a[i].h=;
a[i].vis=true;
a[i].id=i;
}
n+=m;
sort(a,a+n,cmp1); q[]=a[];
int top=;
for(int i=;i<n;i++){
if(a[i].vis==false){
while(top && xieLv(a[i],q[top])<xieLv(q[top],q[top-]))
top--;
q[++top]=a[i];
}
else{
int tmp=top;
while(tmp && xieLv(a[i],q[tmp])<xieLv(a[i],q[tmp-]))
tmp--;
a[i].angle1=xieLv(a[i],q[tmp]); }
} q[]=a[n-];
top=;
for(int i=n-;i>=;i--){
if(a[i].vis==false){
while(top && xieLv(a[i],q[top])<xieLv(q[top],q[top-]))
top--;
q[++top]=a[i];
}
else{
int tmp=top;
while(tmp && xieLv(a[i],q[tmp])<xieLv(a[i],q[tmp-]))
tmp--;
a[i].angle2=xieLv(a[i],q[tmp]); }
} sort(a,a+n,cmp2);
printf("Case #%d\n",++ac);
for(int i=;i<n;i++){
if(a[i].vis){
double ans=PI-atan(a[i].angle1)-atan(a[i].angle2);
printf("%.10lf\n",ans*/PI);
}
} }
return ;
}
 第二种方法是先预处理,再二分查找
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 500006
#define PI acos(-1.0)
int n,m;
struct Node{
double x,h;
}a[N];
int L[N];
int R[N];
double b[N];
bool cmp1(Node a,Node b){
return a.x<b.x;
}
int main()
{
int ac=;
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%lf%lf",&a[i].x,&a[i].h);
}
sort(a,a+n,cmp1);
memset(L,-,sizeof(L));
memset(R,-,sizeof(R));
for(int i=;i<n;i++){
for(int j=i-;j>=;j--){
if(a[j].h>a[i].h){
L[i]=j;
break;
}
}
for(int j=i+;j<n;j++){
if(a[i].h<a[j].h){
R[i]=j;
break;
}
}
}
for(int i=;i<n;i++){
b[i]=a[i].x;
} scanf("%d",&m);
printf("Case #%d:\n",++ac);
for(int i=;i<m;i++){
double x;
scanf("%lf",&x);
int index=lower_bound(b,b+n,x)-b;
int you=index;
double angle1=;
double angle2=;
while(R[you]!=-){
double w=a[you].h/(a[you].x-x);
if(w>angle1){
angle1=w;
}
you=R[you];
}
double w=a[you].h/(a[you].x-x);
if(w>angle1){
angle1=w;
} int zuo=index-;
while(L[zuo]!=-){
double w=a[zuo].h/(x-a[zuo].x);
if(w>angle2){
angle2=w;
}
zuo=L[zuo];
}
w=a[zuo].h/(x-a[zuo].x);
if(w>angle2){
angle2=w;
} double ans=PI-atan(angle1)-atan(angle2); printf("%.10lf\n",ans*/PI);
} }
return ;
}

hdu 5033 Building (单调栈 或 暴力枚举 )的更多相关文章

  1. HDU 5033 Building(单调栈)

    HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...

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

    题意:有一排建筑,每座建筑有一定的高度,宽度可以忽略,求在某点的平地上能看到天空的最大角度. 网上的做法基本都是离线的...其实这道题是可以在线做的. 对于向右能看到的最大角度,从右往左倍增维护每个时 ...

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

    盗张图:来自http://blog.csdn.net/xuechelingxiao/article/details/39494433 题目大意:有一排建筑物坐落在一条直线上,每个建筑物都有一定的高度, ...

  4. hdu - 5033 - Building(单调栈)

    题意:N 幢楼排成一列(1<=N<=10^5),各楼有横坐标 xi(1<=xi<=10^7) 以及高度 hi(1<=hi<=10^7),在各楼之间的Q个位置(1&l ...

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

    做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time L ...

  6. HDU 5033 Building (维护单调栈)

    题目链接 题意:n个建筑物,Q条询问,问所在的位置,看到天空的角度是多少,每条询问的位置左右必定是有建筑物的. 思路 : 维护一个单调栈,将所有的建筑物和所有的人都放到一起开始算就行,每加入一个人,就 ...

  7. hdu 5033 模拟+单调优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5033 平面上有n个建筑,每个建筑由(xi,hi)表示,m组询问在某一个点能看到天空的视角范围大小. 维护一个凸包 ...

  8. Largest Rectangle in a Histogram HDU - 1506 (单调栈)

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  9. HDU - 1248 寒冰王座 数学or暴力枚举

    思路: 1.暴力枚举每种面值的张数,将可以花光的钱记录下来.每次判断n是否能够用光,能则输出0,不能则向更少金额寻找是否有能够花光的.时间复杂度O(n) 2.350 = 200 + 150,买350的 ...

随机推荐

  1. maven编写主代码与测试代码

    3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目主代码位于src/main/ja ...

  2. HDU 3622 Bomb Game(2-sat)

    HDU 3622 Bomb Game 题目链接 题意:求一个最大半径,使得每一个二元组的点任选一个,能够得到全部圆两两不相交 思路:显然的二分半径,然后2-sat去判定就可以 代码: #include ...

  3. unity3d 学习笔记(一)

    操作:按下shit 点击坐标轴中心 切换透视图 动画烘焙的概念:相当于把原来的控制器动画或者IK(骨骼)动画所有塌陷为逐帧动画,导出的时候必须选这一项 着色器:从技术的角度来看,着色器是渲染器的一个部 ...

  4. C++内存分配的五种方法

    在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储区. 栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区.里面的变量通常是局部变量.函数参数 ...

  5. MVC4 成员资格、 身份验证

    SimpleMembership,成员资格提供程序. 通用的提供者和新的 ASP.NET 4.5 Web 窗体和 ASP.NET MVC 4 模板 ASP.NET MVC 4 互联网模板中添加一些新的 ...

  6. 关于SetCapture() 和 ReleaseCapture()的使用方法

    查MSND,对SetCapture()函数的说明为:“该函数在属于当前线程的指定窗体里设置鼠标捕获.一旦窗体捕获了鼠标,全部鼠标输入都针对该窗体,不管光标是否在窗体的边界内.同一时刻仅仅能有一个窗体捕 ...

  7. javaweb中去除某个get方式的参数,并且返回路径

    String requestURL = request.getRequestURL() + ""; // String queryString = request.getQuery ...

  8. java基础之成员变量与局部变量

    成员变量的含义 局部变量的含义 成员变量与局部变量的区别

  9. ORA-01652:无法通过128(在表空间TEMP中)扩展temp段

    在Oracle数据库中进行order by or group by.索引的创建和重创建.distinct操作.union & intersect & minus sort-merge ...

  10. 安装软件配置VC++环境时常出现的问题--Error 1935.安装程序集

    装很多软件是都要配置VC++环境的,但由于系统注册表限制,很多时候软件安装过程中会报如下错误 安装 vc++2005 运行库 Error 1935.安装程序集 Microsoft.vc80.atl,t ...