hdu 5251 包围点集最小矩形 ***

时间:2023-03-09 08:17:33
hdu 5251 包围点集最小矩形 ***

题意:小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些矩形包围起来的面积最小的矩形的面积是多少。

求个凸包,矩形的边一定在凸包上,枚举边,求最大值,即为所求,多年不拍几何,直接套了个模板

以后还得练练

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
typedef double typev;
const double eps = 1e-;
const int N = ;
int sign(double d){
return d < -eps ? - : (d > eps);
}
struct point{
typev x, y;
void in()
{
scanf("%lf%lf",&x,&y);
}
point operator-(point d){
point dd;
dd.x = this->x - d.x;
dd.y = this->y - d.y;
return dd;
}
point operator+(point d){
point dd;
dd.x = this->x + d.x;
dd.y = this->y + d.y;
return dd;
}
void read(){ scanf("%lf%lf", &x, &y); }
}ps[N],pd[N];
int n, cn;
double dist(point d1, point d2){
return sqrt(pow(d1.x - d2.x, 2.0) + pow(d1.y - d2.y, 2.0));
}
double dist2(point d1, point d2){
return pow(d1.x - d2.x, 2.0) + pow(d1.y - d2.y, 2.0);
}
bool cmp(point d1, point d2){
return d1.y < d2.y || (d1.y == d2.y && d1.x < d2.x);
}
//st1-->ed1叉乘st2-->ed2的值
typev xmul(point st1, point ed1, point st2, point ed2){
return (ed1.x - st1.x) * (ed2.y - st2.y) - (ed1.y - st1.y) * (ed2.x - st2.x);
}
typev dmul(point st1, point ed1, point st2, point ed2){
return (ed1.x - st1.x) * (ed2.x - st2.x) + (ed1.y - st1.y) * (ed2.y - st2.y);
}
//多边形类
struct poly{
static const int N = ; //点数的最大值
point ps[N+]; //逆时针存储多边形的点,[0,pn-1]存储点
int pn; //点数
poly() { pn = ; }
//加进一个点
void push(point tp){
ps[pn++] = tp;
}
//第k个位置
int trim(int k){
return (k+pn)%pn;
}
void clear(){ pn = ; }
};
//返回含有n个点的点集ps的凸包
poly graham(point* ps, int n){
sort(ps, ps + n, cmp);
poly ans;
if(n <= ){
for(int i = ; i < n; i++){
ans.push(ps[i]);
}
return ans;
}
ans.push(ps[]);
ans.push(ps[]);
point* tps = ans.ps;
int top = -;
tps[++top] = ps[];
tps[++top] = ps[];
for(int i = ; i < n; i++){
while(top > && xmul(tps[top - ], tps[top], tps[top - ], ps[i]) <= ) top--;
tps[++top] = ps[i];
}
int tmp = top; //注意要赋值给tmp!
for(int i = n - ; i >= ; i--){
while(top > tmp && xmul(tps[top - ], tps[top], tps[top - ], ps[i]) <= ) top--;
tps[++top] = ps[i];
}
ans.pn = top;
return ans;
}
//求点p到st->ed的垂足,列参数方程
point getRoot(point p, point st, point ed){
point ans;
double u=((ed.x-st.x)*(ed.x-st.x)+(ed.y-st.y)*(ed.y-st.y));
u = ((ed.x-st.x)*(ed.x-p.x)+(ed.y-st.y)*(ed.y-p.y))/u;
ans.x = u*st.x+(-u)*ed.x;
ans.y = u*st.y+(-u)*ed.y;
return ans;
}
//next为直线(st,ed)上的点,返回next沿(st,ed)右手垂直方向延伸l之后的点
point change(point st, point ed, point next, double l){
point dd;
dd.x = -(ed - st).y;
dd.y = (ed - st).x;
double len = sqrt(dd.x * dd.x + dd.y * dd.y);
dd.x /= len, dd.y /= len;
dd.x *= l, dd.y *= l;
dd = dd + next;
return dd;
}
//求含n个点的点集ps的最小面积矩形,并把结果放在ds(ds为一个长度是4的数组即可,ds中的点是逆时针的)中,并返回这个最小面积。
double getMinAreaRect(point* ps, int n, point* ds){
int cn, i;
double ans;
point* con;
poly tpoly = graham(ps, n);
con = tpoly.ps;
cn = tpoly.pn;
if(cn <= ){
ds[] = con[]; ds[] = con[];
ds[] = con[]; ds[] = con[];
ans=;
}else{
int l, r, u;
double tmp, len;
con[cn] = con[];
ans = 1e40;
l = i = ;
while(dmul(con[i], con[i+], con[i], con[l])
>= dmul(con[i], con[i+], con[i], con[(l-+cn)%cn])){
l = (l-+cn)%cn;
}
for(r=u=i = ; i < cn; i++){
while(xmul(con[i], con[i+], con[i], con[u])
<= xmul(con[i], con[i+], con[i], con[(u+)%cn])){
u = (u+)%cn;
}
while(dmul(con[i], con[i+], con[i], con[r])
<= dmul(con[i], con[i+], con[i], con[(r+)%cn])){
r = (r+)%cn;
}
while(dmul(con[i], con[i+], con[i], con[l])
>= dmul(con[i], con[i+], con[i], con[(l+)%cn])){
l = (l+)%cn;
}
tmp = dmul(con[i], con[i+], con[i], con[r]) - dmul(con[i], con[i+], con[i], con[l]);
tmp *= xmul(con[i], con[i+], con[i], con[u]);
tmp /= dist2(con[i], con[i+]);
len = xmul(con[i], con[i+], con[i], con[u])/dist(con[i], con[i+]);
if(sign(tmp - ans) < ){
ans = tmp;
ds[] = getRoot(con[l], con[i], con[i+]);
ds[] = getRoot(con[r], con[i+], con[i]);
ds[] = change(con[i], con[i+], ds[], len);
ds[] = change(con[i], con[i+], ds[], len);
}
}
}
return ans+eps;
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
int tt;
scanf("%d",&tt);
int ca=;
while(tt--)
{
printf("Case #%d:\n",ca++);
scanf("%d",&n);
for(i=;i<*n;i++)
{
ps[i].in();
}
double q=getMinAreaRect(ps,*n,pd);
printf("%d\n",int(q+0.5));
}
}