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