HDU 5033 Building 暴力+单调栈优化

Building

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2603 Accepted Submission(s): 739
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 xi with its height hi. 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, xi(1<=xi<=10^7) and hi(1<=hi<=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 qi, 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

题意:
在一条数轴上有n个大楼,给出n个大楼的坐标x和高度h有n个询问,每次询问给出一个x0表示观测点的坐标,求该观测点能观测到的天空的角度。

分析:
用单调栈来维护一个暴力的顺序,从观测点向两边的建筑进行暴力枚举每一个建筑的最大视角(不是枚举所有的建筑,是按照高度递增的顺序枚举,因为越远但高度却不能更高的话,观测角度肯定更大,我们要求观测角度最小),直到枚举到两边最高的建筑为止。

代码:

#include <cstdio>
#include <stack>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

const double PI = acos(-1);

int n;

struct node{
    double x, h;
    node(){}
    node(double xx, double hh):x(xx),h(hh){}
}s[100010];

bool cmp(node a, node b)
{
    return a.x < b.x;
}

int bef[100010], nex[100010], lm[100010], rm[100010];

stack<int> ss;

void init()
{
    memset(bef, -1, sizeof(bef));
    memset(nex, -1, sizeof(nex));
    while(!ss.empty())
        ss.pop();
    for(int i = 0; i < n; i++)
    {
        if(ss.empty() || s[ss.top()].h > s[i].h)
            ss.push(i);
        else
        {
            while(!ss.empty() && s[ss.top()].h < s[i].h)
            {
                nex[ss.top()] = i;
                ss.pop();
            }
            ss.push(i);
        }
    }
    while(!ss.empty())
        ss.pop();
    for(int i = n - 1; i >= 0; i--)
    {
        if(ss.empty() || s[ss.top()].h > s[i].h)
            ss.push(i);
        else
        {
            while(!ss.empty() && s[ss.top()].h < s[i].h)
            {
                bef[ss.top()] = i;
                ss.pop();
            }
            ss.push(i);
        }
    }
    int mh = 0;
    for(int i = 0; i < n; i++)
    {
        if(s[mh].h <= s[i].h)
            mh = i;
        lm[i] = mh;
    }
    mh = n - 1;
    for(int i = n - 1; i >= 0; i--)
    {
        if(s[mh].h <= s[i].h)
            mh = i;
        rm[i] = mh;
    }
}

void debug()
{
    for(int i = 0; i < n; i++)
    {
        printf("%d x = %f h = %f\n", i, s[i].x, s[i].h);
        printf("bef %d\nnex %d\nlm %d\nrm %d\n\n", bef[i], nex[i], lm[i], rm[i]);
    }
}

int main(void)
{
    int t, q;
    double x, h, g;
    scanf("%d", &t);
    for(int cas = 1; cas <= t; cas++)
    {
        printf("Case #%d:\n", cas);
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
             scanf("%lf%lf", &x, &h);
             s[i] = node(x, h);
        }
        sort(s, s + n, cmp);
        init();
        //debug();
        scanf("%d", &q);
        while(q--)
        {
            scanf("%lf", &g);
            int r = lower_bound(s, s + n, node(g, 0), cmp) - s;
            int l = r - 1;
            double tl = 0, tr = 0;
            for(int i = l; i != -1 && i >= lm[l]; i = bef[i])
                tl = max(s[i].h / (g - s[i].x), tl);
            for(int i = r; i != -1 && i <= rm[r]; i = nex[i])
                tr = max(s[i].h / (s[i].x - g), tr);
            double ans = 180 - 180 * (atan(tl) + atan(tr)) / PI;
            printf("%.10f\n", ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值