HDU-5033:Building(单调栈)

Building

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

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+100;
const int MOD=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
struct lenka
{
    double x,h;
    int dx;
}a[MAX],b[MAX];
double ans[MAX];
int q[MAX];
int cmp(const lenka& p,const lenka& q){return p.x<q.x;}
double Y(lenka y1,lenka y2){return y1.h-y2.h;}
double X(lenka x1,lenka x2){return x1.x-x2.x;}
int main()
{
    int T,cas=1;
    cin>>T;
    while(T--)
    {
        int n,m;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%lf%lf",&a[i].x,&a[i].h);
        sort(a+1,a+n+1,cmp);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%lf",&b[i].x);
            b[i].dx=i;
            b[i].h=0;
        }
        sort(b+1,b+m+1,cmp);
        memset(ans,0,sizeof ans);
        int R=0,last=1;
        for(int i=1;i<=m;i++)
        {
            while(last<=n&&a[last].x<b[i].x)
            {
                while(R&&a[q[R]].h<=a[last].h)R--;
                while(R>=2&&Y(a[q[R-1]],a[last])*X(a[last],a[q[R]])-Y(a[q[R]],a[last])*X(a[last],a[q[R-1]])>0)R--;
                q[++R]=last++;
            }
            while(R>=2&&Y(a[q[R-1]],b[i])*X(b[i],a[q[R]])-Y(a[q[R]],b[i])*X(b[i],a[q[R-1]])>0)R--;
            ans[b[i].dx]+=atan((b[i].x-a[q[R]].x)/a[q[R]].h);
        }
        R=0,last=n;
        for(int i=m;i>=1;i--)
        {
            while(last>=1&&a[last].x>b[i].x)
            {
                while(R&&a[q[R]].h<=a[last].h)R--;
                while(R>=2&&Y(a[q[R-1]],a[last])*X(a[q[R]],a[last])-Y(a[q[R]],a[last])*X(a[q[R-1]],a[last])>0)R--;
                q[++R]=last--;
            }
            while(R>=2&&Y(a[q[R-1]],b[i])*X(a[q[R]],b[i])-Y(a[q[R]],b[i])*X(a[q[R-1]],b[i])>0)R--;
            ans[b[i].dx]+=atan((a[q[R]].x-b[i].x)/a[q[R]].h);
        }
        printf("Case #%d:\n",cas++);
        for(int i=1;i<=m;i++)printf("%.10lf\n",180*ans[i]/PI);
    }
    return 0;
}




猜你喜欢

转载自blog.csdn.net/mitsuha_/article/details/80247269