嘘~ 正在从服务器偷取页面 . . .

UVA12307 Smallest Enclosing Rectangle 题解


UVA12307 Smallest Enclosing Rectangle 题解

upd.20220429 由于q779太菜,导致代码出了点锅,已经重新修改

题目链接:UVA12307 Smallest Enclosing Rectangle

题意:多组数据,给定平面上的 \(n\) 个点,分别求出周长最小、面积最小的矩形,使得所有的点均被矩形覆盖

数据范围: \(3\le n \le 10^5\)\(-10^5 \le x,y \le 10^5\)

看到本题,容易想到 P3187

解法是旋转卡壳

要求的是矩形,因此考虑维护三个点:

\(a\) 代表当前枚举直线对面的边

\(b\) 代表当前枚举直线右侧最远的点

\(c\) 代表当前枚举直线左侧最远的点

如下图所示

其中,\(a\) 使用叉积比较,\(b,c\) 则使用点积比较

方便起见,我们称当前的底边长为 \(\text{T}\) (即图中的q[1]到q[2])

可以发现 \(\text{T = L}+\text{R}-\text{d}\)

\(\text{L}\) 表示向量 \(s_i\to c\)\(\text{T}\) 上投影向量的模长

\(\text{R}\) 表示向量 \(s_{i-1} \to b\)\(\text{T}\) 上投影向量的模长

\(\text{d}\) 表示向量 \(s_{i-1}\to s_i\) 的模长(即图中的绿色段)

那么面积就可以计算出来了

关于周长的计算

通过向量旋转和数乘运算来求出矩形的四个顶点坐标

然后直接用两点距离公式计算即可

因为周长和面积不一定同时最小,所以只要两个都分别fmin一下就好了

时间复杂度 \(O(n\log n)\)

代码如下

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF ((int)0x3f3f3f3f3f3f3f3f)
#define inf ((int)0xc0c0c0c0c0c0c0c0)
#define N (int)(1e5+15)
int n;
int stk[N],used[N],top;
const double eps=1e-10;
#define pf(x) ((x)*(x))
struct vct{double x,y;}p[N],s[N],q[15];
vct operator+(vct a,vct b){return {a.x+b.x,a.y+b.y};}
vct operator-(vct a,vct b){return {a.x-b.x,a.y-b.y};}
vct operator*(vct a,double b){return {a.x*b,a.y*b};}
vct operator/(vct a,double b){return {a.x/b,a.y/b};}
double len(vct a){return sqrt(pf(a.x)+pf(a.y));}
double cross(vct a,vct b){return a.x*b.y-a.y*b.x;}
double dot(vct a,vct b){return a.x*b.x+a.y*b.y;}
double dis(vct a,vct b){return sqrt(pf(a.x-b.x)+pf(a.y-b.y));}
double sq1(vct a,vct b,vct c){return fabs(cross(c-a,b-a));}
double sq2(vct a,vct b,vct c){return 0.5*sq1(a,b,c);}
double angle(vct a,vct b){return acos(dot(a,b)/len(a)/len(b));}
vct rot(vct a){return {-a.y,a.x};} // 逆时针旋转90度
void gettb()
{
    sort(p+1,p+1+n,[](vct a,vct b)
    {
        return a.x==b.x?a.y<b.y:a.x<b.x;
    });
    stk[++top]=1;
    for(int i=2; i<=n; i++)
    {
        while(top>1&&cross(p[stk[top]]-p[stk[top-1]],p[i]-p[stk[top]])<=0)
            used[stk[top--]]=0;
        used[i]=1;
        stk[++top]=i;
    }
    int tmp=top;
    for(int i=n-1; i>=1; i--)
    {
        if(used[i])continue;
        while(top>tmp&&cross(p[stk[top]]-p[stk[top-1]],p[i]-p[stk[top]])<=0)
            used[stk[top--]]=0;
        used[i]=1;
        stk[++top]=i;
    }
    for(int i=1; i<=top; i++)
        s[i]=p[stk[i]];
}
void getmn()
{
    int a,b,c;
    double ans1=1e100,ans2=1e100;
    a=b=c=2;
    for(int i=2; i<=top; i++)
    {
        while(cross(s[a%top+1]-s[i],s[i-1]-s[i])>=cross(s[a]-s[i],s[i-1]-s[i]))
            a=a%top+1;
        while(dot(s[b%top+1]-s[i],s[i-1]-s[i])<=dot(s[b]-s[i],s[i-1]-s[i]))
            b=b%top+1;
        if(i==2)c=a;
        while(dot(s[c%top+1]-s[i-1],s[i]-s[i-1])<=dot(s[c]-s[i-1],s[i]-s[i-1]))
            c=c%top+1;
        double d=dis(s[i],s[i-1]);
        double L=fabs(dot(s[c]-s[i],s[i]-s[i-1])/d);
        double R=fabs(dot(s[b]-s[i-1],s[i-1]-s[i])/d);
        double H=fabs(sq1(s[i],s[i-1],s[a])/len(s[i]-s[i-1]));
        double res1=(L+R-d)*H;
        double res2=0;
        q[1]=s[i]-(s[i]-s[i-1])*(L/d);
        q[2]=q[1]+(s[i]-s[i-1])*((R+L-d)/d);
        q[3]=q[2]+rot(q[2]-q[1])*(H/(L+R-d));
        q[4]=q[3]+rot(q[3]-q[2])*((L+R-d)/H);
        for(int k=1; k<=4; k++)
            res2+=dis(q[k],q[k%4+1]);
        ans1=fmin(ans1,res1);
        ans2=fmin(ans2,res2);
    }
    cout << ans1 << " " << ans2 << endl;
}
void clear()
{
    for(int i=1; i<=n; i++)
        used[i]=0;
    top=0;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cout << fixed << setprecision(2);
    // freopen("check.out","w",stdout);
    while(cin >> n&&n!=0)
    {
        clear();   
        for(int i=1; i<=n; i++)
            cin >> p[i].x >> p[i].y;
        gettb();getmn();
    }
    return 0;
}

友情提醒:多测不清空,爆零两行泪!

部分参考了 这篇博客 的写法,但是他写的那个挂了,会出现除以0的情况

代码确实蛮难写的,这道题我研究了一整天才搞出来 QAQ


文章作者: q779
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明来源 q779 !
评论
  目录