题解:P7000 [NEERC 2013] Easy Geometry

· · 题解

距离刷完 XX000 还差一题。

注意到,答案关于矩形宽度单峰。这是因为凸多边形的上下凸壳斜率单调。

注意到,固定矩形宽度,答案关于 xmin 单峰。同样是因为凸多边形的上下凸壳斜率单调。

当然严谨证明很麻烦,如果有大佬写出来了欢迎私信我。

于是考虑三分套三分:先三分矩形宽度,再三分矩形宽度,这样我们只需计算该凸多边形被给定直线 x=x_0 截得的长度即可。枚举每一条边,交点可以用直线的点斜式计算。时间复杂度 O(n\log^2n)。

需要注意精度问题,本奶龙一直被卡也是没办法了,直接枚举 100 种 eps 组合分别计算答案取最大值。

::::success[code]

#include<bits/stdc++.h>
#define db long double
using namespace std;
int n,c,i;
db L=2e9,R=-2e9,s,t,l,r,m1,m2,xmin,xmax,ymin,ymax,XMIN,YMIN,XMAX,YMAX,eps[4],x[100005],y[100005];
db h(db l,db r){
    vector<db>a,b;
    for(int i=0;i<n;i++){
        if((l-x[i])*(l-x[(i+1)%n])<=0)a.push_back(y[i]+(l-x[i])*(y[(i+1)%n]-y[i])/(x[(i+1)%n]-x[i]));
        if((r-x[i])*(r-x[(i+1)%n])<=0)b.push_back(y[i]+(r-x[i])*(y[(i+1)%n]-y[i])/(x[(i+1)%n]-x[i]));
    }
    ymin=max(min(a[0],a[1]),min(b[0],b[1]));
    ymax=min(max(a[0],a[1]),max(b[0],b[1]));
    return max(ymax-ymin,(db)0);
}
db g(db x){
    db l=L,r=R-x,m1,m2;
    while(l+eps[2]<r){
        m1=(l+r)/2-eps[3];
        m2=(l+r)/2+eps[3];
        if(h(m1,m1+x)<h(m2,m2+x))l=m1;
        else r=m2;
    }
    xmin=(l+r)/2;
    xmax=(l+r)/2+x;
    return h((l+r)/2,(l+r)/2+x)*x;
}
db f(){
    l=0;
    r=R-L;
    while(l+eps[0]<r){
        m1=(l+r)/2-eps[1];
        m2=(l+r)/2+eps[1];
        if(g(m1)<g(m2))l=m1;
        else r=m2;
    }
    return g((l+r)/2);
}
int main(){
    cin.tie(0)->sync_with_stdio(0);
    c=clock()+CLOCKS_PER_SEC*0.9;
    cin>>n;
    for(i=0;i<n;i++){
        cin>>x[i]>>y[i];
        L=min(L,x[i]);
        R=max(R,x[i]);
    }
    for(eps[0]=1e-5;eps[0]>=1e-9;eps[0]/=10){
        for(eps[1]=eps[0]/10;eps[1]>=1e-9;eps[1]/=10){
            for(eps[2]=1e-5;eps[2]>=1e-9;eps[2]/=10){
                for(eps[3]=eps[2]/10;eps[3]>=1e-9;eps[3]/=10){
                    s=f();
                    if(s>t){
                        t=s;
                        XMIN=xmin;
                        YMIN=ymin;
                        XMAX=xmax;
                        YMAX=ymax;
                    }
                    if(clock()>c)goto g;
                }
            }
        }
    }
    g:cout<<fixed<<setprecision(9)<<XMIN<<' '<<YMIN<<' '<<XMAX<<' '<<YMAX;
}

::::