hdu 5033 Building

本文介绍了一种使用单调栈解决左右仰角问题的方法,通过维护递增和递减的栈来高效地计算每个观测点的最大仰角,适用于算法竞赛及实际场景中的角度计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//单调栈,和单调队列类似,求左边仰角时维护一个建筑高度呈凸型递减的栈,求右仰角维护高度呈凸型递增的栈

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值