HDU 5033-B - Building-维护凸包-单调栈

http://acm.hdu.edu.cn/showproblem.php?pid=5033


题意:给一个数轴, 是地面。

地面上有n座楼,给出m个人,分别站在m个位置上,问每个位置能看见多大角度的天空(二维平面)



数据范围:

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(1<=N<=10^5), the number of buildings. 

In the following N lines, each line contains two numbers, x  i(1<=x  i<=10^7) and h  i(1<=h  i<=10^7). 

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.

可以根据求凸包的过程求解,把人当成高度为0的楼插入数据中。

第一遍,按下标从左到右扫描,对于当前这个楼,如果与top楼形成的角度的绝对值,比楼top-1与楼top形成的角度绝对值还小(可用叉积判),就把top楼弹出,也就是,单调栈里维护的是角度绝对值增大的建筑群,这样能保证每个人看到的视野都是正确的。

同理,把下标反过来扫一遍,得到是右边的视野角度。

最后加起来就是答案



复杂度O(n)

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
const double PI=acos(-1.0);
struct node
{
    double h;
    double x;
    int id;
    node() {}
    node (double a,double b)
    {
        x=a,h=b;
    }
    node operator - (const node &t)const
    {
        return node(x-t.x,h-t.h);
    }
    int operator * (const node &t)const  /// 叉积 : 有向面积
    {
        return x*t.h-h*t.x;
    }
};
node tm[100123*2];
int top;
double ans[100123*2];
node aa[100123*2];

bool cmp(node a,node b)
{
    return a.x<b.x;
}
bool judge(const node& a,const node& b,const node &c)
{
    return ((b-a)*(c-b))>=0;
}
double angle(const node &a, const node &b)
{
    return atan((double)a.h / (double)(b.x - a.x));
}

int main()
{


    int n,i;
    int t;
    cin>>t;
    int cnt=1;
    while(t--)
    {
        memset(ans,0,sizeof (ans));
        cin>>n;
        for (int i=1; i<=n; i++)
        {
            scanf("%lf%lf",&aa[i].x,&aa[i].h);
        }
        int cun=n;
        int q;
        cin>>q;
        for (int i=1; i<= q; i++)
        {
            scanf("%lf",&aa[++cun].x);
            aa[cun].h=0;
            aa[cun].id=i;
        }
        sort(aa+1,aa+1+cun,cmp);

        int top=0;
        for (int i=1; i<=cun; i++)
        {
            while(top>=2 && judge (tm[top-1],tm[top],aa[i]))
                top--;
            if (aa[i].h>0)
                tm[++top]=aa[i];
            else
                ans[aa[i].id]+=angle(tm[top],aa[i]);
        }
        reverse(aa+1, aa+1+cun);
          top=0;
          for (int i=1;i<=cun;i++)
            aa[i].x=1e7-aa[i].x;
        for (int i=1; i<=cun; i++)
        {
            while(top>=2 && judge (tm[top-1],tm[top],aa[i]))
                top--;
            if (aa[i].h>0)
                tm[++top]=aa[i];
            else
                ans[aa[i].id]+=angle(tm[top],aa[i]);
        }

        printf("Case #%d:\n",cnt++);
        for (int i=1;i<=q;i++)
        printf("%.10lf\n", (PI - ans[i]) / PI * 180.0);//ans[i]存的是看不到的角度

    }

    return 0;

}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值