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. 初入Python继承

    1.什么是继承? 新类不用从头编写 新类从现有的类继承,就自动拥有了现有类的所有功能 新类只需要编写现有类缺少的新功能 2.继承的好处 复用已有代码 自动拥有了现有类的所有功能 只需要编写缺少的新功能 ...

  2. 【Spark】Spark的Shuffle机制

    MapReduce中的Shuffle 在MapReduce框架中,shuffle是连接Map和Reduce之间的桥梁,Map的输出要用到Reduce中必须经过shuffle这个环节,shuffle的性 ...

  3. BNU10806:请在此处签到

    每年圣诞,ZUN都会邀请很多人到幻想乡举行联欢,今年也不例外.在联欢前,所有人需要在自己的昵称旁签到(签全名),以示出席.然后ZUN 会把大家的签到表保存下来作为纪念,以激励来年努力工作.   昵称: ...

  4. Windows CMD命令之tasklist及taskkill

    Tasklist介绍 Tasklist"是 winxp/win2003/vista/win7/win8下的命令,用来显示运行在本地或远程计算机上的所有进程,带有多个执行参数. 使用格式 ta ...

  5. (转)将wcf 以webservice的方式调用

    将wcf 以webservice的方式调用 问题:a公司使用wcf 发布服务(.net Framework 3.0 or 3.5),b公司需要使用a公司发布的服务 ,但b公司目前阶段只使用.net F ...

  6. NSDictionary使用小结

    http://blog.csdn.net/ms2146/article/details/8656787

  7. html5新特性--音频视频,拖放

    1.音频 <audio controls> <source src="aaa.ogg" type="audio/ogg"> <so ...

  8. POJ 1011 - Sticks DFS+剪枝

    POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    ...

  9. <转>请戒掉成功学和正能量,那是麻痹人的毒药 | 新知

    非常不幸的是,这将是一场非常糟糕的演说.我不想骗你们,你们从我这里几乎什么也学不到.你们在离开的时候肯定会感到失望,你们的生活并不会得到改善. 更糟糕的是,你还会意识到生活的本质毫无意义,你的一切努力 ...

  10. linux查看压缩包的文件列表

    网上看到了一篇文章: Using bzip2 with less 这篇文章介绍了一个脚本,脚本功能就是列出压缩包所压缩的文件,本文算是原文搬运,不过减少点东西以适用我日常系统运用. #!/bin/ba ...