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. Spring整合Shiro做权限控制模块详细案例分析

    1.引入Shiro的Maven依赖 <!-- Spring 整合Shiro需要的依赖 --> <dependency> <groupId>org.apache.sh ...

  2. 将Maven项目转换成Eclipse支持的Java项目

    当我们通过模版(比如最简单的maven-archetype-quikstart插件)生成了一个maven的项目结构时,如何将它转换成eclipse支持的java project呢? 1. 定位到mav ...

  3. JavaScript 判断一个字符串是否在另一个字符串中

    传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. start ...

  4. c++11 : range-based for loop

    0. 形式 for ( declaration : expression ) statement 0.1 根据标准将会扩展成这样的形式: 1   { 2     auto&& __ra ...

  5. html与css的移动端与pc端需要注意的事项

    一个移动端与pc端之间最主要的也就是尺寸问题,苹果与安卓的机型尺寸大小相差甚多,一个尺寸都会影响用户的体验.那么我们来了解一下一些常用的解决方法. 一般在网页中都会在头部有一些这样的代码 <me ...

  6. edmx文件

    MethodBase 提供有关方法的信息 在System.Reflector命名空间之下 edmx edmx:Runtime节点下包含与EF有关的定义与映射信息 edmx:ConceptualMode ...

  7. SpinLock(自旋锁)

    SpinLock(自旋锁) SpinLock 结构是一个低级别的互斥同步基元,它在等待获取锁时进行旋转. 在多核计算机上,当等待时间预计较短且极少出现争用情况时,SpinLock 的性能将高于其他类型 ...

  8. android内存优化之图片压缩和缓存

    由于手机内存的限制和网络流量的费用现在,我们在加载图片的时候,必须要做好图片的压缩和缓存. 图片缓存机制一般有2种,软引用和内存缓存技术. 1.压缩图片:压缩图片要既不能模糊,也不能拉伸图片. 图片操 ...

  9. Lanucherr 默认显示第几屏

    Launcher.java static final int SCREEN_COUNT = 5;static final int DEFAULT_SCREEN = 2;//第一页是从0开始计数,这里是 ...

  10. ORACLE SQL单行函数(一)【weber出品必属精品】

    1.SUBSTR:求父串中的子串 SUBSTR('HelloWorld',1,5) 1:代表子串的起始位置,如果为正,正数,如果为负,倒数 5:代表字串的终止位置,只能向右数,可以省略,如果省略就是数 ...