hdu 5033 Building(单调栈)

Building

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2807    Accepted Submission(s): 784
Special Judge


Problem Description
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).
 

Source

题意是求某一点处能看到的最大的视角。

分析:

  在离观察点最大视角的房子的左边的房子是不会影响视角的,因此可以低于某一房子之前的房子都清除

 于是只有两种情况要考虑

   1.当出现该图情况时,中间凹进去的点是不用考虑进视角的计算中的,因此可以除去该点


 2.当中间的点是凸出来的时候,是要考虑的,因为在4的左边是由3房子影响视角,4-5是由2房子影响视角,5的右边是由1房子影响视角

  分别求两边的左右房子的位置就可以了

  小心该题的输入是实数,用浮点数来保存
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cmath>
#define MAX_N 100005
using namespace std;
const double pai=acos(-1.0);
struct node
{
    double x,y;
    int l,r,id;
}q[2*MAX_N];
double cal(node a,node b)
{
    return (a.y-b.y)/(a.x-b.x);
}
bool cmp(node a,node b)
{
    return a.x<b.x;
}
bool cmp2(node a,node b)
{
    return a.id<b.id;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int k=1;k<=t;k++)
    {
        int n1,n2;
        scanf("%d",&n1);
        for(int i=1;i<=n1;i++)
        {
            scanf("%lf%lf",&q[i].x,&q[i].y);
            q[i].id=i;
        }
        scanf("%d",&n2);
        for(int i=n1+1;i<=n1+n2;i++)
        {
            scanf("%lf",&q[i].x);
            q[i].y=0,q[i].id=i;
        }
        sort(q+1,q+1+n1+n2,cmp);
        stack <node> sta;
        sta.push(q[1]);
        for(int i=2;i<=n1+n2;i++)
        {
            while(!sta.empty()&&q[i].y>=sta.top().y) sta.pop();
            while(sta.size()>1)
            {
                node tmp=sta.top();
                sta.pop();
                double tan1=cal(tmp,sta.top());
                double tan2=cal(tmp,q[i]);
                if(tan2<tan1) {sta.push(tmp);break;}
            }
            if(q[i].id>n1) q[i].l=sta.top().id;
            sta.push(q[i]);
        }
        while(!sta.empty()) sta.pop();
        sta.push(q[n1+n2]);
        for(int i=n1+n2-1;i>=1;i--)
        {
            while(!sta.empty()&&q[i].y>=sta.top().y) sta.pop();
            while(sta.size()>1)
            {
                node tmp=sta.top();
                sta.pop();
                double tan1=cal(tmp,sta.top());
                double tan2=cal(tmp,q[i]);
                if(tan2>tan1) {sta.push(tmp);break;}
            }
            if(q[i].id>n1) q[i].r=sta.top().id;
            sta.push(q[i]);
        }
        sort(q+1,q+1+n1+n2,cmp2);
        double ang;
        printf("Case #%d:\n",k);
        for(int i=1;i<=n1+n2;i++)
        {
            if(q[i].id>n1)
            {
                double ang1=-atan(cal(q[q[i].l],q[i]))/pai*180;
                double ang2=atan(cal(q[q[i].r],q[i]))/pai*180;
                ang=180-ang1-ang2;
                printf("%.10lf\n",ang);
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值