HDU5033 building

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(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.
Output
For each test case, first output one line "Case #x:", where x is the case number (starting from 1). 

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 10^(-4).
Sample Input
3
3
1 2
2 1
5 1
1
4
3
1 3
2 2
5 1
1
4
3
1 4
2 3
5 1
1
4
Sample Output
Case #1:
101.3099324740
Case #2:
90.0000000000
Case #3:
78.6900675260
这个题对我来说好难啊 只能比着写尽量理解一下了
1.问的是每个点能看到的角度 一个点肯定实在两个楼之间的 所以是180-左边的角度-右边的角度
2.怎么求角度 现在可以得到高度之差与横坐标之差 就用反正切函数atan 但注意它返回的是弧度值要转换成角度
3.一个点左右两边的角度是什么 
没图说不清 反正他一边的楼要呈凸函数状才对点产生影响 画图发现无论从左还是从右斜率的绝对值都逐渐增大 
4.点跟楼在一起根据x坐标排序 然后维护单调栈 用id记录原来的位置 
 
 
  
  
#include <iostream>
#include<stack>
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn=200010;
const double PI=3.1415926535898;
double ans[maxn];
struct node
{
    double x,h;
    int id;
}a[maxn];
double cmp(node a,node b)
{
    return a.x<b.x;
}
void cal(int n)
{
    stack<node>s;
    s.push(a[0]);
    for(int i=1;i<n;i++)
    {

        while(s.size()>=2)
        {
            node tmp=s.top();
            s.pop();
            double xielv1=(tmp.h-a[i].h)/(tmp.x-a[i].x);
            double xielv2=(tmp.h-s.top().h)/(tmp.x-s.top().x);
            //cout<<xielv1<<" "<<xielv2<<endl;
            if(xielv1<xielv2)
            {
                s.push(tmp);
                break;
            }
        }
        if(a[i].h==0)
        {
            ans[a[i].id]=atan(fabs((a[i].h-s.top().h)/(a[i].x-s.top().x)))/PI*180;
            //cout<<ans[a[i].id]<<endl;
        }
        s.push(a[i]);
    }
}
void cal1(int n)
{
    stack<node>s;
    s.push(a[n-1]);
    for(int i=n-1;i>=0;i--)
    {

        while(s.size()>=2)
        {
            node tmp=s.top();
            s.pop();
            double xielv1=(tmp.h-a[i].h)/(tmp.x-a[i].x);
            double xielv2=(tmp.h-s.top().h)/(tmp.x-s.top().x);
            if(xielv1>xielv2)
            {
                s.push(tmp);
                break;
            }
        }
        if(a[i].h==0)
        {
            ans[a[i].id]+=atan(fabs((a[i].h-s.top().h)/(a[i].x-s.top().x)))/PI*180;
        }
        s.push(a[i]);
    }
}
int main()
{
    int t;
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        int n;
        memset(ans,0,sizeof(ans));
        scanf("%d",&n);
        for(int j=0;j<n;j++)
        scanf("%lf%lf",&a[j].x,&a[j].h),a[j].id=0;
        int q;
        scanf("%d",&q);
        for(int j=0;j<q;j++)
        {
            scanf("%lf",&a[n].x);
            a[n].h=0;
            a[n].id=j;
            n++;
            ans[j]=0.0;
        }
        sort(a,a+n,cmp);
        cal(n);
        cal1(n);
        printf("Case #%d:\n",i);
        for(int j=0;j<q;j++)
        {
            printf("%.10lf\n",180-ans[j]);
        }

    }
    return 0;
}

    
    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值