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

比赛的时候没想到,看了别人的题解,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;
}
时间: 2024-08-02 02:51:20

hdu 5033 Building(北京网络赛)的相关文章

[hdu 5032]2014北京网络赛Always Cook Mushroom 离散化+离线线段树/树状数组

Always Cook Mushroom Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 196    Accepted Submission(s): 54 Problem Description Matt has a company, Always Cook Mushroom (ACM), which produces high-q

HDU 5033 Building(北京网络赛B题)

HDU 5033 Building 题目链接 思路:利用单调栈维护建筑建的斜线,保持斜率单调性,然后可以把查询当成高度为0的建筑,和建筑和在一起考虑,从左往右和从右往左各扫一遍即可 代码: #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <algorithm> using namespace std; const int N = 200

[2014 北京网络赛]

02 hdu 5033 Building 题目意思: 数轴上有n根柱子,每根柱子有个位置坐标和高度,有q个询问,询问从位置qi能看到的角度(保证左右至少有一个柱子) 解题思路: 单调栈维护一个凸性柱子序列. 离线处理所有的查询,排序,然后扫一遍qi,把柱子插进去,更新单调栈.注意查询位置也要更新栈. 代码: //#include<CSpreadSheet.h> #include<iostream> #include<cmath> #include<cstdio&g

2015北京网络赛A题The Cats&#39; Feeding Spots

题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<memory.h> 6 using namespace std; 7 const i

2015北京网络赛 Couple Trees 倍增算法

2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道.  解法来自 qscqesze ,这个其实之前如果了解过倍增的话还是不是很难,不过这题的数据也不是很给力,极限数据理论上是过不了的.  其他解法有树链剖分?并不是很清楚.就这样水过了吧... 1 #include <iostream> 2 #include <cstdio> 3 #include &l

hihocoder1236(北京网络赛J):scores 分块+bitset

北京网络赛的题- -.当时没思路,听大神们说是分块+bitset,想了一下发现确实可做,就试了一下,T了好多次终于过了 题意: 初始有n个人,每个人有五种能力值,现在有q个查询,每次查询给五个数代表查询的五种能力值,输出有多少个人每种能力值都比查询的小 n和q都是50000,每种能力值最大也为50000 思路: 对于某一个大小的能力值,有哪些人的此项能力值比他小可以用一个50000的bitset表示.这样我们在查询的时候就可以拿到5个对应的bitset,对其进行and就可以得出最终的人数 这样每

2015北京网络赛 D-The Celebration of Rabbits 动归+FWT

2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x)为满足x≤ai≤m+x且ai的异或和为0 的序列a的个数. 求 ∑Rx=Lf(x)mod1000000007 思路:因为对于每一个第一次分配后的a序列对应唯一的x,所以我们就枚举x然后在求序列的个数.

hdu 5033 Building(单调性+二分)

题目链接:hdu 5033 Building 题目大意:城市里有n座摩天大厦,给定每栋大厦的位置和高度,假定大厦的宽度为0.现在有q次查询,表示人站的位置,人的高度视为0,问说可以仰望天空的角度. 解题思路:比赛的时候用单调性优化直接给过了,对于每个大厦来说,记录左右两边与其形成斜率最大的大厦序号以及斜率,然后每次查询是,通过二分确认人所在位置属于哪两栋大厦之间,然后分别向左向右确认角度,在确认角度时,根据大厦记录的最大斜率进行判断. 以左边为例, 然后对于查询位置x: 这种解法仅仅是优化查询效

HDU 5024 (广州网络赛) Wang Xifeng&#39;s Little Plot 记忆化搜索+枚举

Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. This novel was created in Qing Dynasty, by Cao Xueqin.

HDU-4041-Eliminate Witches! (11年北京网络赛!!)

Eliminate Witches! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1124    Accepted Submission(s): 426 Problem Description Kaname Madoka is a Magical Girl(Mahou Shoujo/Puella Magi). The duty of