hdu 5033 Building(北京网络赛)

Building

                                                           Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
                                                                                          Total Submission(s): 1090    Accepted Submission(s): 309
                                                                                                                                        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).
 

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
 

比赛的时候没想到,看了别人的题解,http://blog.csdn.net/u011345136/article/details/39454537
方法太巧妙了,由于查询的位置没有建筑,把查询的位置和建筑物的位置放在一个数组里,把查询的位置特殊标记一下,取查询次序的相反值,这样不仅标记了查询的次序,又与建筑物分开了,最后从左往又扫一遍,取当前位置左边的斜率的最大值,从右往左扫一遍,取当前位置右边斜率的最大值。这样就可以算最大视角了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn=100000+100;
const double pi = acos(-1.0);
struct node
{
    int x;
    int h;
}a[maxn*2],stacks[maxn*2];
double ans[maxn];
int n,q;
bool cmp(node a,node b)
{
    return a.x<b.x;
}
int cross(node a,node b,node c)//判断角的大小
{
    if(c.h<0)
    c.h=0;
    return (long long)(c.x-b.x)*(a.h-c.h)>=(long long)(c.x-a.x)*(b.h-c.h);
}
double getangle(node a,node b)
{
    return atan((double)(b.x-a.x)*1.0/(double)(a.h*1.0));
}
void solve()
{
    int head=0;
    for(int i=0;i<n+q;i++)
    {
        if(a[i].h<=0)
        {
            while(head>=2&&cross(stacks[head-2],stacks[head-1],a[i]))
            {
                head--;
            }
            ans[-a[i].h]+=getangle(stacks[head-1],a[i]);
        }
        else
        {
            while(head&&stacks[head-1].h<=a[i].h)
            {
                head--;
            }
            while(head>=2&&cross(stacks[head-2],stacks[head-1],a[i]))
            {
                head--;
            }
            stacks[head++]=a[i];
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    int ca=1;
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].h);
        }
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            scanf("%d",&a[i+n].x);
            a[i+n].h=-i;
        }
        sort(a,a+n+q,cmp);
        memset(ans,0,sizeof(ans));
        solve();
        reverse(a,a+n+q);//逆置
        for(int i=0;i<n+q;i++)//从右往左
        a[i].x = 10000000 - a[i].x;//保证前面的位置小于后面的,由于ans中存储的是查询的位置,不会影响答案。
        solve();
        printf("Case #%d:\n",ca++);
        for(int i=0;i<q;i++)
        {
            printf("%.10f\n",ans[i]*180.0/pi);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值