1. 程式人生 > >HDU 5033 Building 暴力+單調棧優化

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