[bzoj1038][ZJOI2008]瞭望塔

来自FallDream的博客,未经允许,请勿转载,谢谢


致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安。我们将H村抽象为一维的轮廓。如下图所示 我们可以用一条山的上方轮廓折线(x1, y1), (x2, y2), …. (xn, yn)来描述H村的形状,这里x1 < x2 < …< xn。瞭望塔可以建造在[x1, xn]间的任意位置, 但必须满足从瞭望塔的顶端可以看到H村的任意位置。可见在不同的位置建造瞭望塔,所需要建造的高度是不同的。为了节省开支,ditoly村长希望建造的塔高度尽可能小。请你写一个程序,帮助dadzhi村长计算塔的最小高度。n<=300

 

先半平面交求一个下凸壳,然后答案只可能在凸壳的拐角或者轮廓线的拐角处,枚举更新答案

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define MN 300
#define eps 1e-10
#define INF 1e17
using namespace std;
inline int read()
{
    int x = 0 , f = 1; char ch = getchar();
    while(ch < '0' || ch > '9'){ if(ch == '-') f = -1;  ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}

struct P{
    double x,y;
    P(double _x=0,double _y=0):x(_x),y(_y){}
    P operator+(P b){return P(x+b.x,y+b.y);}
    P operator-(P b){return P(x-b.x,y-b.y);}
    P operator*(double b){return P(x*b,y*b);}
    double operator^(P b){return x*b.y-b.x*y;}
}p[MN+5];

struct L{
    P p,v;double slop;
    L(){}
    L(P x,P y):p(x),v(y){slop=atan2(y.y,y.x);}
    P operator*(L b){P u=p-b.p;double t=(b.v^u)/(v^b.v);return p+v*t;}
    bool Left(P b){return (v^(b-p))>eps;}
    bool operator<(const L&b)const{return slop<b.slop;}
}q[MN+5],s[MN+5];

int n,top,tail;
double x[MN+5],y[MN+5],ans=INF;

void solve()
{
    q[top=tail=1]=s[1];
    for(int i=2;i<=n+3;i++)
    {
        while(top>tail&&!s[i].Left(p[top]))--top;
        while(top>tail&&!s[i].Left(p[tail+1])) ++tail;
        if(fabs(s[i].slop-q[top].slop)<eps)
            q[top]=s[i].Left(q[top].p)?q[top]:s[i];
        else q[++top]=s[i];
        p[top]=q[top]*q[top-1];
    }
    while(top>tail&&!q[tail].Left(p[top])) --top;
}

int main()
{
    n=read();
    for(int i=1;i<=n;i++)scanf("%lf",&x[i]);
    for(int i=1;i<=n;i++)scanf("%lf",&y[i]);
    for(int i=1;i<n;i++)s[i]=L(P(x[i],y[i]),P(x[i+1]-x[i],y[i+1]-y[i]));
    s[n]=L(P(x[n],INF),P(-INF*2,0));
    s[n+1]=L(P(x[1],-INF),P(INF*2,0));
    s[n+2]=L(P(x[1],INF),P(0,-2*INF)); 
    s[n+3]=L(P(x[n],-INF),P(0,INF*2));
    sort(s+1,s+n+4);
    solve();p[tail]=q[tail]*q[top];int j;
    for(int i=tail;i<=top;i++) 
    {
        if(p[i].x<=x[1]+eps&&p[i].x+eps>=x[n]) continue;
        for(j=1;j<n;j++) if(x[j]<=p[i].x+eps&&p[i].x+eps<=x[j+1]) break;
        P t=L(P(x[j],y[j]),P(x[j+1]-x[j],y[j+1]-y[j]))*L(P(p[i].x,-INF),P(0,2*INF));
        ans=min(ans,p[i].y-t.y);
    }
    for(int i=1;i<=n;i++)
    {
        for(j=tail;j<top;j++) if(p[j].x<=x[i]+eps&&p[j+1].x>=x[i]+eps) break;
        P t=L(p[j],p[j+1]-p[j])*L(P(x[i],-INF),P(0,2*INF));
        ans=min(ans,t.y-y[i]);
    }
    printf("%.3lf",ans);
    return 0;
}

 

FallDream代表秋之国向您问好! 欢迎您来我的博客www.cnblogs.com/FallDream
posted @ 2017-04-05 18:51  FallDream  阅读(182)  评论(0编辑  收藏  举报