BZOJ 1038 ZJOI 2008 瞭望塔 半平面交

CODE：

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define MAX 310
#define INF 1e11
#define EPS 1e-10
#define DCMP(a) (fabs(a) < EPS ? true:false)
using namespace std;

struct Point{
double x,y;

Point(double _ = .0,double __ = .0):x(_),y(__) {}
Point operator -(const Point &a)const {
return Point(x - a.x,y - a.y);
}
Point operator +(const Point &a)const {
return Point(x + a.x,y + a.y);
}
Point operator *(double a)const {
return Point(x * a,y * a);
}
}point[MAX],p[MAX];

struct Line{
Point p,v;
double alpha;

Line(Point _,Point __):p(_),v(__) {
alpha = atan2(v.y,v.x);
}
Line() {}
bool operator <(const Line &a)const {
return alpha < a.alpha;
}
}line[MAX],q[MAX];

int points,lines;

inline void MakeLine(const Point &a,const Point &b)
{
line[++lines] = Line(a,b - a);
}

inline double Cross(const Point &a,const Point &b)
{
return a.x * b.y - a.y * b.x;
}

inline bool OnLeft(const Line &l,const Point &p)
{
return Cross(l.v,p - l.p) > 0;
}

inline Point GetIntersection(const Line &a,const Line &b)
{
Point u = a.p - b.p;
double temp = Cross(b.v,u) / Cross(a.v,b.v);
return a.p + a.v * temp;
}

int HalfPlaneIntersection()
{
int front = 1,tail = 1;
q[1] = line[1];
for(int i = 2; i <= lines; ++i) {
while(front < tail && !OnLeft(line[i],p[tail - 1]))	--tail;
if(DCMP(Cross(line[i].v,q[tail].v)))
q[tail] = OnLeft(line[i],q[tail].p) ? q[tail]:line[i];
else	q[++tail] = line[i];
if(front < tail)	p[tail - 1] = GetIntersection(q[tail],q[tail - 1]);
}
return tail;
}

inline double GetAns(int cnt)
{
double ans = INF;
for(int i = 1; i < cnt; ++i) {
double x = p[i].x;
int pos;
for(int j = 1;j < points; ++j)
if(x <= point[j].x) {
pos = j;
break;
}
Point intersection = GetIntersection(Line(point[pos - 1],point[pos] - point[pos - 1]),Line(p[i],Point(0,1)));
ans = min(ans,p[i].y - intersection.y);
}
for(int i = 1; i <= points; ++i) {
double temp = .0;
for(int j = 1; j <= cnt; ++j) {
Point intersection = GetIntersection(q[j],Line(point[i],Point(0,1)));
temp = max(temp,intersection.y - point[i].y);
}
ans = min(ans,temp);
}
return ans;
}

int main()
{
cin >> points;
for(int i = 1; i <= points; ++i)
scanf("%lf",&point[i].x);
for(int i = 1; i <= points; ++i)
scanf("%lf",&point[i].y);
for(int i = 1; i < points; ++i)
MakeLine(point[i],point[i + 1]);
sort(line + 1,line + lines + 1);
int cnt = HalfPlaneIntersection();
cout << fixed << setprecision(3) << GetAns(cnt) << endl;
return 0;
}

posted @ 2017-04-23 14:11  jzdwajue  阅读(104)  评论(0编辑  收藏  举报