设为首页 加入收藏

TOP

HDU 5033 Building (单调栈)
2015-07-20 17:37:59 来源: 作者: 【 】 浏览:2
Tags:HDU 5033 Building 单调

?

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

Source 2014 ACM/ICPC Asia Regional Beijing Online

?

题意:

给出N幢摩天大楼的位置和高度,有Q次查询,问在某点处能看见天空的角度范围。

分析:

一开始的想法是维护一个单调栈,首先离线数据并排序,然后左右扫两边。单调栈内大楼的高度要递减,然后判断栈顶元素是否比它的前一个更优,如果不是则出栈。但是如果栈顶元素比它前一个优,并不能保证是全局最优的。

正确的维护方法是保证单调栈内的大楼高度递减,而且要使得这些大楼的最高点构成一个类似凸包的形状,或者说是凸包的一部分,然后再判断栈顶元素和它前一个比是否更优,如果不是则出栈。这样最后的栈顶元素就是待查询点在某方向的视角最大范围,记录下这个方向向量即可,最后角度只要求两向量间的夹角即可。

?

(ugly code,maybe I could attend ioccc)

?

/*
 *
 *  Author  :  fcbruce
 *
 *  Date  :  2014-09-21 13:07:59 
 *
 */
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 #include
                 #include 
                 
                   #define sqr(x) ((x)*(x)) #define LL long long #define itn int #define INF 0x3f3f3f3f #define PI 3.1415926535897932384626 #define eps 1e-10 #ifdef _WIN32 #define lld %I64d #else #define lld %lld #endif #define maxm 100010 #define maxn 100010 using namespace std; struct node { double x,h; bool operator < (const node &n)const { return x
                  
                    &v1,const pair
                   
                     &v2) { return v1.first*v2.first+v1.second*v2.second; } inline double len(const pair
                    
                      &v) { return sqrt(sqr(v.first)+sqr(v.second)); } pair
                     
                       v_l[maxn],v_r[maxn]; int st[maxn]; int main() { #ifdef FCBRUCE freopen(/home/fcbruce/code/t,r,stdin); #endif // FCBRUCE int T_T,__=0; scanf(%d,&T_T); while (T_T--) { int n,m; scanf(%d,&n); for (int i=0;i
                      
                       -1 && sky[j].h>sky[st[top]].h-eps) top--; while (top>0 && (sky[st[top]].h-sky[j].h)*(sky[st[top]].x-sky[st[top-1]].x)< (sky[st[top-1]].h-sky[st[top]].h)*(sky[j].x-sky[st[top]].x)+eps) top--; st[++top]=j++; while (top>0 && sky[st[top]].h*(q[i].x-sky[st[top-1]].x)< sky[st[top-1]].h*(q[i].x-sky[st[top]].x)+eps) top--; } while (top>0 && sky[st[top]].h*(q[i].x-sky[st[top-1]].x)< sky[st[top-1]].h*(q[i].x-sky[st[top]].x)+eps) top--; v_l[q[i].h]=make_pair(sky[st[top]].x-q[i].x,sky[st[top]].h); } for (int i=m-1,j=n-1,top=-1;i>=0;i--) { while (j>=0 && sky[j].x>q[i].x) { while (top>-1 && sky[j].h>sky[st[top]].h-eps) top--; while (top>0 && (sky[st[top]].h-sky[j].h)*(sky[st[top-1]].x-sky[st[top]].x)< (sky[st[top-1]].h-sky[st[top]].h)*(sky[st[top]].x-sky[j].x)+eps) top--; st[++top]=j--; while (top>0 && sky[st[top]].h*(sky[st[top-1]].x-q[i].x)< sky[st[top-1]].h*(sky[st[top]].x-q[i].x)+eps) top--; } while (top>0 && sky[st[top]].h*(sky[st[top-1]].x-q[i].x)< sky[st[top-1]].h*(sky[st[top]].x-q[i].x)+eps) top--; v_r[q[i].h]=make_pair(sky[st[top]].x-q[i].x,sky[st[top]].h); } printf(Case #%d: ,++__); for (int i=0;i
                       
                        

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDOJ 5038 Grade 下一篇SGU 199. Beautiful People 二维L..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)