HDU 5033 - Building(几何 + 单调栈)

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

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

                                                                     

题意:

在x 轴上,给出N(1<=N<=10^5)条直线的xi(1<=xi<=10^7)坐标和高度hi(1<=hi<=10^7),多次询问Q(1<=Q<=10^5),求在位置(Xi,0)看到的天空的角度。

思路:

维护 相邻两建筑顶(xi,hi)的连线的斜率的绝对值上升 的单调栈。

将建筑的坐标和询问位置一起放入结构体中按x从小到大排列。

从左往右扫一遍:维护单调栈,用一个结构体m维护,栈里建筑高度为递减,且斜率为递增。当访问到h = 0 的点,则记录此时最大斜率得到的角度的信息。

再从右到左扫一遍。

要注意的一点是:从左往右访问和从右往左访问函数check()里边是不一样的。

CODE:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;

const double PI = acos(-1.0);
int N, M;
double ans[100010];
struct node
{
    double x, h;
    int id1, id2;
}n[200010], m[200010];

bool cmp(node a, node b)
{
    return a.x < b.x;
}
bool cmp1(node a, node b)
{
    return a.id1 < b.id1;
}
double get_angle(node a, node b, int tt)
{
    if(tt == 1) return atan((b.x - a.x) / (a.h));
    if(tt == 2) return atan((a.x - b.x) / (a.h));
}
double check(node a, node b, node c)
{
    if((ll)(c.h - a.h) * (b.x - a.x) >= (ll)(b.h - a.h) * (c.x - a.x))
        return 0;
    return 1;
}
void cal()
{
    int t = 0, nn = 0;
    for(int i = 0; i < N + M; i ++){
        if(n[i].h == 0.0){
            while(t > 1 && check(n[i], m[t - 1], m[t - 2])) t--;
            n[i].id2 = nn;
            ans[nn ++] += get_angle(m[t - 1], n[i], 1);
        }
        else{
            while(t > 0 && n[i].h >= m[t - 1].h) t --;
            while(t > 1 && check(n[i], m[t - 1], m[t - 2])) t --;
            m[t++] = n[i];
        }
    }
    t = 0;
    memset(m, 0, sizeof(m));
    nn = M;
    for(int i = N + M - 1; i >= 0; i --){
        if(n[i].h == 0.0){
            while(t > 1 && check(n[i], m[t - 2], m[t - 1])) t--; //check里面和上面是不一样的。
            ans[-- nn] += get_angle(m[t - 1], n[i], 2);
        }
        else{
            while(t > 0 && n[i].h >= m[t - 1].h) t --;
            while(t > 1 && check(n[i], m[t - 2], m[t - 1])) t --;
            m[t++] = n[i];
        }
    }
}
int main()
{
    freopen("in", "r", stdin);
    int T;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ca ++){
        printf("Case #%d:\n", ca);
        memset(ans, 0, sizeof(ans));
        memset(n, 0, sizeof(n));
        memset(m, 0, sizeof(m));
        scanf("%d", &N);
        for(int i = 0; i < N; i ++){
            scanf("%lf %lf", &n[i].x, &n[i].h);
        }
        scanf("%d", &M);
        int j = 0;
        for(int i = N; i < N + M; i ++){
            scanf("%lf", &n[i].x);
            n[i].h = 0.0;
            n[i].id1 = i;
        }
        sort(n, n + N + M, cmp);
        cal();
        sort(n, n + N + M, cmp1);

        for(int i = N; i < M + N; i ++){
            printf("%.10lf\n", ans[n[i].id2] * 180.0 / PI);
        }
    }
    return 0;
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值