您的位置:首页 > 产品设计 > UI/UE

hdu 5033 Building 几何+单调栈

2014-09-21 19:35 274 查看
          大概用一个单调栈存一下大楼的编号以及这个大楼可以控制的范围....然后乱搞一下...被虐了一下午精神有点恍惚,先贴代码,具体思路闲了再补......WA了一下午,晚上加了四行直接过了......
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
typedef double type;
using namespace std;
const double PI=acos(-1.0);
const double eps=1e-5;
const double inf=1e18;
struct Point
{
type x,y;
Point(){}
Point(type a,type b)
{
x=a;
y=b;
}
void read()
{
scanf("%lf%lf",&x,&y);
}
void print()
{
printf("%.6lf %.6lf\n",x,y);
}

};
typedef Point Vector;
Vector operator + (Vector A,Vector B)
{
return Vector(A.x+B.x,A.y+B.y);
}
Vector operator - (Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator * (Vector A,type p)
{
return Vector(A.x*p,A.y*p);
}
Vector operator / (Vector A,type p)
{
return Vector(A.x/p,A.y/p);
}
bool operator < (const Point &a,const Point &b)
{
return a.x<b.x;
// return a.x<b.x || (a.x==b.x && a.y<b.y);
}

int dcmp(double x)
{
if (fabs(x)<eps) return 0;
else return x<0?-1:1;
}
bool operator == (const Point& a,const Point b)
{
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;
}
//atan2(x,y) :向量(x,y)的极角,即从x轴正半轴旋转到该向量方向所需要的角度。
type Dot(Vector A,Vector B)
{
return A.x*B.x+A.y*B.y;
}
type Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
type Length(Vector A)
{
return sqrt(Dot(A,A));
}
type Angle(Vector A,Vector B)
{
return acos(Dot(A,B))/Length(A)/Length(B);
}

type Area2(Point A,Point B,Point C)
{
return Cross(B-A,C-A);
}
Vector Rotate(Vector A,double rad)
{
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}

Vector Normal(Vector A)//单位法线,左转90度,长度归一
{
double L=Length(A);
return Vector(-A.y/L,A.x/L);
}

Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
const int maxn=500050;
int n,m;
Point p[maxn];
int lt[maxn],rt[maxn];
struct query
{
double x;
int id;
}qy[maxn];
bool cmpx(query a,query b)
{
return a.x<b.x;
}
bool cmpid(query a,query b)
{
return a.id<b.id;
}
struct node
{
int x;
double pos;
node(int xx,double y)
{
x=xx;
pos=y;
}
node()
{

}
};
int main()
{
//    freopen("in.txt","r",stdin);
int tt;
int cnt=0;
scanf("%d",&tt);
while(tt--)
{
memset(lt,-1,sizeof lt);
memset(rt,-1,sizeof rt);
scanf("%d",&n);
for (int i=0; i<n; i++)
p[i].read();
sort(p,p+n);

scanf("%d",&m);
for (int i=0; i<m; i++)
{
scanf("%lf",&qy[i].x);
qy[i].id=i;
}
sort(qy,qy+m,cmpx);

stack<node> s;
while(!s.empty()) s.pop();

int i=-1,j=0;
Point ins;

for (j=0; j<m; j++)
{
double v=qy[j].x;
int id;
while (i+1<n && dcmp(p[i+1].x-v)==-1)
{
i++;
while(!s.empty())
{
id=s.top().x;
if (dcmp(p[id].y-p[i].y)!=1) s.pop();
else break;
}

if (s.empty())
{
s.push(node(i,inf));
}
else
{

while(!s.empty())
{
id=s.top().x;
Point p1=p[id];
Vector v1=p[i]-p[id];
Point p2=Point(p[id].x,0);
Vector v2=Vector(1,0);
ins=GetLineIntersection(p1,v1,p2,v2);
if (ins.x<s.top().pos) break;
else s.pop();
}
s.push(node(i,ins.x));
}
}

id=qy[j].id;
while(!s.empty())
{
if (dcmp(s.top().pos-v)==1)
{
lt[id]=s.top().x;
break;
}
else s.pop();
}

if (s.empty())
{
lt[id]=-1;
}
}

while(!s.empty()) s.pop();

i=n;
for (j=m-1; j>=0; j--)
{
double v=qy[j].x;

int id;
while(i-1>=0 && dcmp(p[i-1].x-v)==1)
{
i--;
while(!s.empty())
{
id=s.top().x;
if (dcmp(p[id].y-p[i].y)!=1)s.pop();
else break;
}
if (s.empty())
{
s.push(node(i,-inf));
}
else
{
while(!s.empty())
{
id=s.top().x;
Point p1=p[id];
Vector v1=p[i]-p[id];
Point p2=Point(p[id].x,0);
Vector v2=Vector(-1,0);
ins=GetLineIntersection(p1,v1,p2,v2);
if (ins.x>s.top().pos) break;
else s.pop();
}
s.push(node(i,ins.x));
}
}
id=qy[j].id;
while(!s.empty())
{
if (dcmp(s.top().pos-v)==-1)
{
rt[id]=s.top().x;
break;
}
else s.pop();
}
if (s.empty()) rt[id]=-1;
}

printf("Case #%d:\n",++cnt);
sort(qy,qy+m,cmpid);
for (int i=0; i<m; i++)
{
double a1,a2;
Vector v1,v2;
v2=Vector(0.0,1.0);
if (lt[i]==-1) a1=90.0;
else
{
v1=p[lt[i]]-Point(qy[i].x,0);
v1=v1/Length(v1);
a1=Angle(v1,v2)*180.0/PI;
}
if (rt[i]==-1) a2=90.0;
else
{
v1=p[rt[i]]-Point(qy[i].x,0);
v1=v1/Length(v1);
a2=Angle(v1,v2)*180.0/PI;
}
printf("%.12lf\n",a1+a2);
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: