[BZOJ1038][ZJOI2008]瞭望塔(半平面交)

题目描述

传送门

题解

首先把底下的直线都用半平面交搞一下,然后上面会形成一个凸壳
那么答案一定在底下的转折点或者凸壳上的定点取到
因为…分段函数的极值一定在分段点?
然后暴力、枚举、求交点…
可关键是这题的精度丧心病狂啊…我的代码精度<=1e-9,半平面交的初始范围必须是1e10或者1e11才对…

代码

#include
#include
#include
#include
#include
using namespace std;
#define N 505

const double eps=1e-9;
const double inf=1e10;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector
{
    double x,y;
    Vector(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
    bool operator < (const Vector &a) const
    {
        return xtypedef Vector Point;
Vector operator + (Vector a,Vector b) {return Vector(a.x+b.x,a.y+b.y);}
Vector operator - (Vector a,Vector b) {return Vector(a.x-b.x,a.y-b.y);}
Vector operator * (Vector a,double b) {return Vector(a.x*b,a.y*b);}

int n,cnt,ncnt;
double ans;
Point p[N],poly[N],npoly[N];

double Dot(Vector a,Vector b)
{
    return a.x*b.x+a.y*b.y;
}
double Cross(Vector a,Vector b)
{
    return a.x*b.y-a.y*b.x;
}
double Len(Vector a)
{
    return sqrt(Dot(a,a));
}
bool insLS(Point A,Point B,Point C,Point D)
{
    int x=dcmp(Cross(C-B,A-B));
    int y=dcmp(Cross(D-B,A-B));
    return x&&y&&x!=y;
}
Point GLI(Point P,Vector v,Point Q,Vector w)
{
    Vector u=P-Q;
    double t=Cross(w,u)/Cross(v,w);
    return P+v*t;
}
void init()
{
    cnt=0;
    poly[++cnt]=Point(inf,inf);
    poly[++cnt]=Point(-inf,inf);
    poly[++cnt]=Point(-inf,-inf);
    poly[++cnt]=Point(inf,-inf);
}
void halfp(Point A,Point B)
{
    Point C,D;
    ncnt=0;
    for (int i=1;i<=cnt;++i)
    {
        C=poly[i%cnt+1];
        D=poly[(i+1)%cnt+1];
        if (dcmp(Cross(C-B,A-B))>=0)
            npoly[++ncnt]=C;
        if (insLS(A,B,C,D))
            npoly[++ncnt]=GLI(A,B-A,C,D-C);
    }
    cnt=ncnt;
    for (int i=1;i<=cnt;++i) poly[i]=npoly[i];
}

int main()
{
    scanf("%d",&n);
    if (n==1) {puts("0.000");return 0;}
    for (int i=1;i<=n;++i) scanf("%lf",&p[i].x);
    for (int i=1;i<=n;++i) scanf("%lf",&p[i].y);
    init();
    for (int i=1;i1]);
    ans=1e60;
    for (int i=1;i<=n;++i)
        for (int j=1;j<=cnt;++j)
            if (dcmp(poly[j%cnt+1].x-p[i].x)<=0&&dcmp(poly[(j+1)%cnt+1].x-p[i].x)>=0)
            {
                Point P=GLI(poly[j%cnt+1],poly[(j+1)%cnt+1]-poly[j%cnt+1],p[i],Vector(0,inf));
                ans=min(ans,Len(P-p[i]));
            }
    for (int i=1;i<=cnt;++i)
        for (int j=1;j<=n;++j)
            if (dcmp(p[j%n+1].x-poly[i].x)<=0&&dcmp(p[(j+1)%n+1].x-poly[i].x)>=0)
            {
                Point P=GLI(p[j%n+1],p[(j+1)%n+1]-p[j%n+1],poly[i],Vector(0,-inf));
                ans=min(ans,Len(P-poly[i]));
            }
    printf("%.3lf\n",ans);
}

你可能感兴趣的:(题解,省选,计算几何)