http://acm.hdu.edu.cn/showproblem.php?pid=5033
详见注释
#include <bits/stdc++.h>
using namespace std;
const double PI=acos(-1.0);
const int maxn=1e5+10;
struct node
{
double x,h;
int tp;
};
map <int,bool> mp;
stack <node> stk;
node ary[2*maxn];
double lef[maxn],rgt[maxn];
int n,q;
bool cmp(node n1,node n2)
{
return n1.x<n2.x;
}
void init(double *pre)//单调栈中维护的是一个类似凸包的东西
{
node tmp1,tmp2;
int i;
while(!stk.empty()) stk.pop();
for(i=1;i<=n;i++){
if(ary[i].tp==0){//如果是建筑
while(!stk.empty()&&stk.top().h<=ary[i].h) stk.pop();//比当前建筑矮的直接去掉 因为这些肯定会被当前建筑挡住而看不见
while(stk.size()>1){//此处维护凸包 比较一下斜率即可
tmp1=stk.top();
stk.pop();
tmp2=stk.top();
stk.push(tmp1);
if((tmp2.h-ary[i].h)*(ary[i].x-tmp1.x)>=(tmp1.h-ary[i].h)*(ary[i].x-tmp2.x)) stk.pop();
else break;
}
stk.push(ary[i]);
}
else{//如果是人
if(stk.empty()){//一览无余
pre[ary[i].tp]=90.0;
}
else{
while(stk.size()>1){//找第一个挡住此人视线的建筑 还是通过斜率维护凸包
tmp1=stk.top();
stk.pop();
tmp2=stk.top();
stk.push(tmp1);
if(tmp2.h*(ary[i].x-tmp1.x)>=tmp1.h*(ary[i].x-tmp2.x)) stk.pop();
else break;
}
tmp1=stk.top();
//printf("*%.2f %.2f*\n",tmp1.x,tmp1.h);
pre[ary[i].tp]=(atan((ary[i].x-tmp1.x)/tmp1.h)/PI)*180.0;//弧度转角度
}
}
}
}
int main()
{
int t,cas,i,tmp;
scanf("%d",&t);
for(cas=1;cas<=t;cas++){
scanf("%d",&n);
for(i=1;i<=n;i++){
ary[i].tp=0;
scanf("%lf%lf",&ary[i].x,&ary[i].h);
}
scanf("%d",&q);
for(i=n+1;i<=n+q;i++){
ary[i].tp=i-n;
scanf("%lf",&ary[i].x);
ary[i].h=0.0;
}
n+=q;//人和建筑一同处理
sort(ary+1,ary+n+1,cmp);
init(lef);
for(i=1;i<=n/2;i++){//翻转后同样处理即可
swap(ary[i],ary[n-i+1]);
}
for(i=1;i<=n;i++){
ary[i].x=10000000.0-ary[i].x;
}
init(rgt);
mp.clear();
for(i=1;i<=n;i++){//猜测可能会有人被建筑挡住的情况 觉得此时应该什么都看不到
if(ary[i].tp==0){
tmp=ary[i].x;
mp[tmp]=1;
}
}
for(i=1;i<=n;i++){
if(ary[i].tp!=0){
tmp=ary[i].x;
if(mp[tmp]){
lef[ary[i].tp]=rgt[ary[i].tp]=0.0;
}
}
}
printf("Case #%d:\n",cas);
for(i=1;i<=q;i++){
printf("%.10f\n",lef[i]+rgt[i]);
}
}
return 0;
}
/*
2
1
0 5
1
0
1
0 1
1
1
*/