【bzoj1038】瞭望塔

题意

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

分析

分析1:模拟退火+二分

调不出来。

调得出来的时候估计阳寿已尽。

有生之年内应该不会调得出来吧。

就放一个半成品代码吧。

#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <algorithm>
using namespace std;

#define rep(i,a,b) for (int i=(a);i<=(b);i++)

const int N=512;

const double MAX=1e11;
const double EPS=1e-6;

const int T=1;
const double SPEED=0.99;
const double END=0.00001;

int rd(void);

int n;
struct point {
    double x,y;
    point(double _x=0,double _y=0) {
        x=_x,y=_y;
    }
    friend point operator - (point a,point b) {
        return point(a.x-b.x,a.y-b.y);
    }
}p[N];
double det(point a,point b) {
    return a.x*b.y-a.y*b.x;
}

double now; double nd;
double ans;

int rd(void) {
    int x=0,f=1; char c=getchar();
    for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
    for (;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*f;
}

int cmp(double a) {
    if (fabs(a)<EPS) return 0;
    return a<0?-1:1;
}

int Check(double xn,double yn) {
    rep(i,2,n) {
        double t=det(p[i-1]-point(xn,yn),p[i]-point(xn,yn));
        if (cmp(t)<0) return 0;
    }
    return 1;
}

int InBorder(double xn,double l,double r) {
    return cmp(xn-l)>=0&&cmp(r-xn)>=0;
}

double Solve(double xn) {
    double l=0,r=MAX;
    while (cmp(r-l)>0) {
        double mid=(l+r)/2;
        if (Check(xn,mid))
            r=mid;
        else l=mid;
    }

    double bsH;
    rep(i,1,n-1)
        if (InBorder(xn,p[i].x,p[i+1].x)) {
            double k=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x),b=p[i].y-k*p[i].x;
            bsH=k*xn+b;
            break;
        }

    return l-bsH;
}

double RandFloat(void) {
    return rand()%1000/1000.0;
}

double SA(void) {
    now=(p[1].x+p[n].x)/2.0;
    nd=Solve(now);
    double res=nd;
    for (double tem=p[n].x-p[1].x;tem>END;tem*=SPEED) {
        double nx=now+tem*(RandFloat()*2-1);
        if (!InBorder(nx,p[1].x,p[n].x)) continue;
        double nxd=Solve(nx); double slack=RandFloat();
        if (cmp(nxd-nd)<0||cmp(tem-slack)>0) {
            now=nx;
            nd=nxd;
            res=min(res,nxd);
        }
    }
    return res;
}

int main(void) {
    #ifndef ONLINE_JUDGE
    freopen("bzoj1038.in","r",stdin);
    freopen("bzoj1038.out","w",stdout);
    #endif

    srand(19980406);

    n=rd();
    rep(i,1,n) p[i].x=rd();
    rep(i,1,n) p[i].y=rd();

    ans=MAX;
    rep(tms,1,T) {
        double t=SA();
        ans=min(ans,t);
    }
    printf("%0.3lf\n",ans);

    return 0;
}

分析2:三分法

对于一条线段,上面的点的答案必然是单峰的。

证明略,自己画个图YY去吧。

有一篇题解:http://blog.csdn.net/Fuxey/article/details/50528819

分析3:半平面交

http://blog.csdn.net/regina8023/article/details/43935773

05-11 03:20