您的位置:首页 > 产品设计 > UI/UE

hdu 5033 Building

2014-09-22 22:46 330 查看
//单调栈,和单调队列类似,求左边仰角时维护一个建筑高度呈凸型递减的栈,求右仰角维护高度呈凸型递增的栈

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<stack>
#define pi 3.1415926535898
using namespace std;
struct point
{
double x,h;int index;
}p[200010],sta[200010];
double ans[100010];
bool cmp(point a,point b)
{
return a.x<b.x;
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
int n;
memset(ans,0,sizeof(ans));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].h);
p[i].index=0;
}
int Q;
scanf("%d",&Q);
for(int i=n;i<n+Q;i++)
{
scanf("%lf",&p[i].x);
p[i].h=0;
p[i].index=i-n+1;
}
sort(p,p+Q+n,cmp);
int top=0;
for(int i=0;i<n+Q;i++)
{
if(p[i].index>0)
{
while(top>1&&sta[top-1].h/(p[i].x-sta[top-1].x)<=(sta[top-2].h-sta[top-1].h)/(sta[top-1].x-sta[top-2].x))top--;
ans[p[i].index]+=atan(sta[top-1].h/(p[i].x-sta[top-1].x))/pi*180;
}
else
{
while(top>1&&(sta[top-1].h-p[i].h)/(p[i].x-sta[top-1].x)<=(sta[top-2].h-sta[top-1].h)/(sta[top-1].x-sta[top-2].x))top--;
sta[top++]=p[i];
}
}
top=0;
for(int i=n+Q-1;i>=0;i--)
{
if(p[i].index>0)
{
while(top>1&&sta[top-1].h/(sta[top-1].x-p[i].x)<=(sta[top-2].h-sta[top-1].h)/(sta[top-2].x-sta[top-1].x))top--;
ans[p[i].index]+=atan(sta[top-1].h/(sta[top-1].x-p[i].x))/pi*180;
}
else
{
while(top>1&&(sta[top-1].h-p[i].h)/(sta[top-1].x-p[i].x)<=(sta[top-2].h-sta[top-1].h)/(sta[top-2].x-sta[top-1].x))top--;
sta[top++]=p[i];
}
}
printf("Case #%d:\n",cas);
for(int i=1;i<=Q;i++)
{
printf("%.10lf\n",180.0-ans[i]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: