您的位置:首页 > 编程语言 > Go语言

hdu5033 单调栈

2014-09-21 20:24 218 查看
题意:给定很多摩天大楼的高度和一些观测点 然后要求求出每一个观测点仰望天空的最大角度

解法:维护一个斜率下降的序列 这样就可以隐去一些凹点 这些凹点是不会影响后来的点的 

#include<set>
#include<map>
#include<cstdio>
#include<iostream>
#include<string.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 555555
#define mid ((l+r)>>1)
#define pi acos(-1.0)
int n,m,co;
double xx,hl,hr;
struct node{
double xi,hi,ansl,ansr;
int id;
}point[maxn],st[maxn];
bool cmp(node x,node y){return x.xi<y.xi;}
int cnt=0;
double ans[maxn],ans1[maxn];
int main(){
int _;scanf("%d",&_);
for(int z=1;z<=_;++z){
printf("Case #%d:\n",z);
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%lf%lf",&point[i].xi,&point[i].hi);
point[i].id=0;
}
cnt=n;
scanf("%d",&m);
for(int i=1;i<=m;++i){
scanf("%lf",&point[cnt].xi);
point[cnt].id=i;
point[cnt].hi=0;
cnt++;
}

for(int i=0;i<cnt;++i)point[i].ansl=-1,point[i].ansr=-1;
sort(point,point+cnt,cmp);
// for(int i=0;i<cnt;++i){
// printf("%lf %lf\n",point[i].xi,point[i].hi);
// }
printf("\n");
int top=0;
for(int i=0;i<cnt;++i){
while(top>=1&&point[i].hi>st[top].hi){
top--;
}
while(top>=2){
double xl=(st[top].hi-point[i].hi)/(point[i].xi-st[top].xi);
if(xl<=st[top].ansl){
top--;
}else{
break;
}
}
st[++top]=point[i];
if(top==1){
st[top].ansl=0;
}else{
st[top].ansl=(st[top-1].hi-st[top].hi)/(st[top].xi-st[top-1].xi);
ans[st[top].id]=st[top].ansl;
// printf("i:%d ansl:%lf id:%d\n",i,st[top].ansl,st[top].id);
}
}top=0;
for(int i=cnt-1;i>=0;--i){
while(top>=1&&point[i].hi>st[top].hi){
top--;
}
while(top>=2){
double xl=(st[top].hi-point[i].hi)/(st[top].xi-point[i].xi);
if(xl<=st[top].ansr){
top--;
}else{
break;
}
}
st[++top]=point[i];
if(top==1){
st[top].ansr=0;
}else{
st[top].ansr=(st[top-1].hi-st[top].hi)/(st[top-1].xi-st[top].xi);
ans1[st[top].id]=st[top].ansr;
// printf("i:%d ansr:%lf id:%d\n",i,st[top].ansr,st[top].id);
}
}
// printf("%lf %lf\n",ans[1],ans1[1]);

for(int i=1;i<=m;++i){
printf("%.10f\n",
(180.0)*(pi/2-atan(ans[i]))/pi+(180.0)*(pi/2-atan(ans1[i]))/pi);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm