HDU5033 building

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.
InputThe 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<=xi<=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. OutputFor 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
這個題對我來說好難啊 只能比着寫儘量理解一下了
1.問的是每個點能看到的角度 一個點肯定實在兩個樓之間的 所以是180-左邊的角度-右邊的角度
2.怎麼求角度 現在可以得到高度之差與橫座標之差 就用反正切函數atan 但注意它返回的是弧度值要轉換成角度
3.一個點左右兩邊的角度是什麼 
沒圖說不清 反正他一邊的樓要呈凸函數狀纔對點產生影響 畫圖發現無論從左還是從右斜率的絕對值都逐漸增大 
4.點跟樓在一起根據x座標排序 然後維護單調棧 用id記錄原來的位置 

#include <iostream>
#include<stack>
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn=200010;
const double PI=3.1415926535898;
double ans[maxn];
struct node
{
    double x,h;
    int id;
}a[maxn];
double cmp(node a,node b)
{
    return a.x<b.x;
}
void cal(int n)
{
    stack<node>s;
    s.push(a[0]);
    for(int i=1;i<n;i++)
    {

        while(s.size()>=2)
        {
            node tmp=s.top();
            s.pop();
            double xielv1=(tmp.h-a[i].h)/(tmp.x-a[i].x);
            double xielv2=(tmp.h-s.top().h)/(tmp.x-s.top().x);
            //cout<<xielv1<<" "<<xielv2<<endl;
            if(xielv1<xielv2)
            {
                s.push(tmp);
                break;
            }
        }
        if(a[i].h==0)
        {
            ans[a[i].id]=atan(fabs((a[i].h-s.top().h)/(a[i].x-s.top().x)))/PI*180;
            //cout<<ans[a[i].id]<<endl;
        }
        s.push(a[i]);
    }
}
void cal1(int n)
{
    stack<node>s;
    s.push(a[n-1]);
    for(int i=n-1;i>=0;i--)
    {

        while(s.size()>=2)
        {
            node tmp=s.top();
            s.pop();
            double xielv1=(tmp.h-a[i].h)/(tmp.x-a[i].x);
            double xielv2=(tmp.h-s.top().h)/(tmp.x-s.top().x);
            if(xielv1>xielv2)
            {
                s.push(tmp);
                break;
            }
        }
        if(a[i].h==0)
        {
            ans[a[i].id]+=atan(fabs((a[i].h-s.top().h)/(a[i].x-s.top().x)))/PI*180;
        }
        s.push(a[i]);
    }
}
int main()
{
    int t;
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        int n;
        memset(ans,0,sizeof(ans));
        scanf("%d",&n);
        for(int j=0;j<n;j++)
        scanf("%lf%lf",&a[j].x,&a[j].h),a[j].id=0;
        int q;
        scanf("%d",&q);
        for(int j=0;j<q;j++)
        {
            scanf("%lf",&a[n].x);
            a[n].h=0;
            a[n].id=j;
            n++;
            ans[j]=0.0;
        }
        sort(a,a+n,cmp);
        cal(n);
        cal1(n);
        printf("Case #%d:\n",i);
        for(int j=0;j<q;j++)
        {
            printf("%.10lf\n",180-ans[j]);
        }

    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章