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. 5狐网教你从零基础做Firefox os 手机应用开发赚money

    如果你还没有接触过web编程,这里有基础教程教你怎样一步一步学习开发,如果你已经是一个web编程基础的人,那你就很容易将web编程放到手机上,轻松教你移植web应用游戏到Firefox手机应用再发布到 ...

  2. Multithreading: How to Use the Synchronization Classes

    (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu 转载请标明来源) 翻译文章来源:  msdn - Multithreading: How to Use t ...

  3. ZigBee心电传输(二)

    不管怎样,还是在高手的帮助下完成了前面的硬件部分,现在进行ZigBee的心电AD采集和转换.需要把ZigBee重新拾起来. 首先明确下目标和思路吧,目标是将模拟心电信号通过AD转换,变为数字信号,再用 ...

  4. [RxJS] Stopping a Stream with TakeUntil

    Observables often need to be stopped before they are completed. This lesson shows how to use takeUnt ...

  5. samba后台进程及安全模式简介

    识别 Samba 后台程序Linux 服务器通常作为守护程序(daemon) 来实现,这一词源于希腊神话,其中守护神(daemon)是超自然生物.Linux 守护程序在后台运行以便执行一些有用的任务. ...

  6. VCS仿真 Dump Memory

    VCS仿真 Dump Memory 两种方法 vcs联合verdi生成fsdb文件 vcs生成vpd文件 VCS联合verdi生成fsdb文件 1.testbench中加入如下语句: initial ...

  7. JS高级程序设计学习笔记之RegExp类型

    创建正则表达式: 字面量形式定义正则表达式: Var expression = / pattern /flags ;pattern部分可以使任意简单或复杂的正则表达式.每个正则表达式可以带有一个或多个 ...

  8. asp.net事件委托易理解实例

    比如说一个公司(场景),你是老板,手下有两个员工,小张和小王. 你命令小王,如果小张玩游戏,则小王扣去小张500元钱.这就是现实中的委托.实际上,在写程序中,程序员就是老板,小张和小王就是两个对象.小 ...

  9. OD调试9—实例:深入分析代码完成软件破解

    OD调试9—实例:深入分析代码完成软件破解  爆破,是最初级的解决方案,不到万不得已,我们不直接修改JNZ通关.因为这样子的话,我们就享受不到破解.逆向的真正乐趣了. 了解程序背后按照剧情发展经常会出 ...

  10. Java中构造方法的执行顺序

    一.先执行内部静态对象的构造方法,如果有多个按定义的先后顺序执行:静态对象在构造的时候也是也先执行其内部的静态对象. 二.再调用父类的构造方法(父类还有父类的话,从最开始的基类开始调用),如果没有明显 ...