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

传送门
题意:给出一个 n n n个点的轮廓,要求找一个高度最小的点使得它能够看见所有拐点。


思路:之间建半平面交然后取半平面交上的每个交点和每个轮廓更新答案即可。
代码:

#include
#define ri register int
using namespace std;
typedef long long ll;
const int N=305;
struct pot{
    double x,y;
    pot(double _x=0,double _y=0):x(_x),y(_y){}
    friend inline pot operator+(const pot&a,const pot&b){return pot(a.x+b.x,a.y+b.y);}
    friend inline pot operator-(const pot&a,const pot&b){return pot(a.x-b.x,a.y-b.y);}
    friend inline double operator^(const pot&a,const pot&b){return a.x*b.y-a.y*b.x;}
    friend inline pot operator*(const double&a,const pot&b){return pot(a*b.x,a*b.y);}
}a[N];
struct L{
    pot a,b;
    double ang;
    L(pot _a=pot(0,0),pot _b=pot(0,0),double _ang=0):a(_a),b(_b),ang(_ang){}
    friend inline bool operator<(const L&a,const L&b){return a.ang==b.ang?((b.b-a.a)^(b.b-b.a))>0:a.ang<b.ang;}
}l[N];
inline pot inter(const L&a,const L&b){
    double k1=(b.b-a.b)^(b.b-a.a),k2=(b.a-a.a)^(b.a-a.b),k=k2/(k1+k2);
    return b.a+k*(b.b-b.a);
}
int n,m;
inline bool check(const L&a,const L&b,const L&c){
    pot tmp=inter(c,b);
    return ((a.b-tmp)^(a.a-tmp))>0;
}
inline void hpi(L a[]){
    static int top;
    sort(l+1,l+n);
    m=0;
    for(ri i=1;i<n;++i)if(i==1||l[i].ang!=l[i-1].ang)l[++m]=l[i];
    top=2;
    for(ri i=3;i<=m;++i){
        while(top>1&&check(l[top-1],l[top],l[i]))--top;
        l[++top]=l[i];
    }
    m=top;
}
inline int read(){
    int ans=0;
    bool f=1;
    char ch=getchar();
    while(!isdigit(ch))f^=(ch=='-'),ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return f?ans:-ans;
}
int main(){
    n=read();
    for(ri i=1;i<=n;++i)a[i].x=read();
    for(ri i=1;i<=n;++i)a[i].y=read();
    for(ri i=2;i<=n;++i)l[i-1]=L(a[i-1],a[i],atan2(a[i].y-a[i-1].y,a[i].x-a[i-1].x));
    hpi(l);
    double ans=1e18;
    for(ri i=1,j=1;i<=n;++i){
        while(j<m&&inter(l[j],l[j+1]).x<a[i].x)++j;
        pot tmp=inter(l[j],L(a[i],pot(a[i].x,-1),0.0));
        ans=min(ans,tmp.y-a[i].y);
    }
    for(ri i=1,j=1;i<m;++i){
        pot tmp=inter(l[i],l[i+1]),upd;
        if(tmp.x<a[1].x)continue;
        while(j<n&&a[j+1].x<tmp.x)++j;
        if(j==n)continue;
        upd=inter(L(a[j],a[j+1]),L(pot(tmp.x,-1),tmp));
        ans=min(ans,tmp.y-upd.y);
    }
    printf("%.3lf",ans);
    return 0;
}

你可能感兴趣的:(#,半平面交)