hdu 5033 ( Building ) 单调栈

152 篇文章 0 订阅
20 篇文章 0 订阅

题意: 给出建筑物位置与高度,以及人的位置,询问人看到天空的最大角度

思路:

对于每个位置,人的视野受到左侧与右侧楼与这个点相连的直线  , 左侧斜率最小的 以及右侧斜率最大的 两条直线限制,如果暴力枚举显然会T

可以处理为一个栈的形式: 

以左侧楼的限制为例:右侧同理

 对于每个位置 如果这个位置是楼,就扔进先除去所有栈中比它高度低的, 再以当前点为基准,所描栈中的点,确保栈中点的到当前点的斜率是递减的(左侧栈是个 凸形),这样就会保证了当前点是卡住人的视野最大的。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
#include<cmath>
using namespace std;

const int N = 3e5 + 50;
const double pi = acos(-1.0);
typedef long long ll;
struct p{
    ll x, h, id;
}a[N], ansl[N], ansr[N];
bool cmp(p a, p b){
    return a.x < b.x;
}
stack<int> s;

int main(){
    int T;
    scanf("%d", &T);
    for(int kase = 1; kase <= T; kase++){
        memset(a, 0, sizeof a);
        memset(ansl, 0, sizeof ansl);
        memset(ansr, 0, sizeof ansr);
        int n, m;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) 
            scanf("%lld %lld", &a[i].x, &a[i].h);
        scanf("%d", &m);
        for(int i = n + 1; i <= n + m; i++) {
            scanf("%lld", &a[i].x);
            a[i].h = -1, a[i].id = i - n;
        }
        int tot = n + m;
        sort(a + 1, a + tot + 1, cmp);
        while(!s.empty()) 
            s.pop();
        for(int i = 1; i <= tot; i++){
            if(a[i].h != -1){
                while(!s.empty() && a[i].h >= a[s.top()].h) s.pop();
                while(s.size() > 1){
                    ll x1 = a[i].x - a[s.top()].x, h1 = a[s.top()].h - a[i].h;
                    int t = s.top();
                    s.pop();
                    ll x2 = a[t].x - a[s.top()].x, h2 = a[s.top()].h - a[t].h;
                    if(h1 * x2 > h2 * x1) {
                        s.push(t);
                        break;
                    }
                }
                s.push(i);
            }else {
                while(s.size() > 1){
                    ll x1 = a[i].x - a[s.top()].x, h1 = a[s.top()].h;
                    int t = s.top();
                    s.pop();
                    ll x2 = a[i].x - a[s.top()].x, h2 = a[s.top()].h;
                    if(h1 * x2 > x1 * h2) {
                        s.push(t);
                        break;
                    }
                }    
                ansl[a[i].id].h = s.top();
                ansl[a[i].id].x = a[i].x;    
            }
        }
        while(!s.empty()) 
            s.pop();
        for(int i = tot; i >= 1; i--){
            if(a[i].h != -1){
                while(!s.empty() && a[s.top()].h <= a[i].h) s.pop();
                while(s.size() > 1){
                    ll x1 = a[s.top()].x - a[i].x, h1 = a[s.top()].h - a[i].h;
                    int t = s.top();
                    s.pop();
                    ll x2 = a[s.top()].x - a[t].x, h2 = a[s.top()].h - a[t].h;
                    if(h1 * x2 > h2 * x1) {
                        s.push(t);
                        break;
                    }
                }
                s.push(i);
            }else {
                while(s.size() > 1){
                    ll x1 = a[s.top()].x - a[i].x, h1 = a[s.top()].h;
                    int t = s.top();
                    s.pop();
                    ll x2 = a[s.top()].x - a[i].x, h2 = a[s.top()].h;
                    if(h1 * x2 > x1 * h2) {
                        s.push(t);
                        break;
                    }
                }    
                ansr[a[i].id].h = s.top();
                ansr[a[i].id].x = a[i].x;    
            }
        }
        printf("Case #%d:\n", kase);
        for(int i = 1; i <= m; i++){
            double j1 = 180 * 1.0 / pi * atan2(1.0 * a[ansl[i].h].h, (ansl[i].x - a[ansl[i].h].x));
            double j2 = 180 * 1.0 / pi * atan2(1.0 * a[ansr[i].h].h, (a[ansr[i].h].x - ansr[i].x));
            printf("%0.10lf\n", 180 - j1 - j2);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值