hdu 5033 Building (单调栈 或 暴力枚举 )

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 ;
}
上一篇:《Linux内核原理与分析》第四次作业


下一篇:WinForm 窗体与窗体相互嵌套