UVA12307 Smallest Enclosing Rectangle 题解

UVA12307 Smallest Enclosing Rectangle 题解

本文同步发表在CSDN和luogu博客上

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

题目链接:UVA12307 Smallest Enclosing Rectangle

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

数据范围: 3 ≤ n ≤ 1 0 5 3\le n \le 10^5 3n105 − 1 0 5 ≤ x , y ≤ 1 0 5 -10^5 \le x,y \le 10^5 105x,y105

看到本题,容易想到 P3187

解法是旋转卡壳

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

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

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

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

如下图所示

在这里插入图片描述

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

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

可以发现 T = L + R − d \text{T = L}+\text{R}-\text{d} T = L+Rd

L \text{L} L 表示向量 s i → c s_i\to c sic T \text{T} T 上投影向量的模长

R \text{R} R 表示向量 s i − 1 → b s_{i-1} \to b si1b T \text{T} T 上投影向量的模长

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

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

关于周长的计算

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

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

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

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

代码如下

#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

转载请说明出处

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值