http://acm.hdu.edu.cn/showproblem.php?pid=5033
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
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.
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).
題意:
給出N幢摩天大樓的位置和高度,有Q次查詢,問在某點處能看見天空的角度範圍。
分析:
一開始的想法是維護一個單調棧,首先離線數據並排序,然後左右掃兩邊。單調棧內大樓的高度要遞減,然後判斷棧頂元素是否比它的前一個更優,如果不是則出棧。但是如果棧頂元素比它前一個優,並不能保證是全局最優的。
正確的維護方法是保證單調棧內的大樓高度遞減,而且要使得這些大樓的最高點構成一個類似凸包的形狀,或者說是凸包的一部分,然後再判斷棧頂元素和它前一個比是否更優,如果不是則出棧。這樣最後的棧頂元素就是待查詢點在某方向的視角最大範圍,記錄下這個方向向量即可,最後角度只要求兩向量間的夾角即可。
(ugly code,maybe I could attend ioccc)
/*
*
* Author : fcbruce
*
* Date : 2014-09-21 13:07:59
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#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<n.x;
}
}sky[maxn];
struct __q
{
double x;
int h;
bool operator < (const __q &q)const
{
return x<q.x;
}
}q[maxm];
inline double
dot(const pair<double,double> &v1,const pair<double,double> &v2)
{
return v1.first*v2.first+v1.second*v2.second;
}
inline double
len(const pair<double,double> &v)
{
return sqrt(sqr(v.first)+sqr(v.second));
}
pair<double,double> 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<n;i++)
scanf("%lf%lf",&sky[i].x,&sky[i].h);
sort(sky,sky+n);
scanf("%d",&m);
for (int i=0;i<m;i++)
{
scanf("%lf",&q[i].x);
q[i].h=i;
}
sort(q,q+m);
for (int i=0,j=0,top=-1;i<m;i++)
{
while (j<n && 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]].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:\n",++__);
for (int i=0;i<m;i++)
printf("%.10f\n",acos(dot(v_l[i],v_r[i])/(len(v_r[i])*len(v_l[i])))*180/PI);
}
return 0;
}