(beginer) 旋转卡壳 UVA 12307 - Smallest Enclosing Rectangle

Problem H

Smallest Enclosing Rectangle

There are n points in 2D space. You're to find a smallest enclosing rectangle of these points. By "smallest" we mean either area or perimeter (yes, you have to solve both problems. The optimal rectangle for these two problems might be different). Note that the sides of the rectangle might not be parallel to the coordinate axes.

Input

There will be at most 10 test cases in the input. Each test case begins with a single integer n (3<=n<=100,000), the number of points. Each of the following n lines contains two real numbers x, y (-100,000<=x,y<=100,000), the coordinates of the points. The points will not be collinear. The last test case is followed by a line with n=0, which should not be processed.

Output

For each line, print the area of the minimum-area enclosing rectangle, and the perimeter of the minimum-perimeter enclosing rectangle, both rounded to two decimal places.

Sample Input

5
0 0
2 0
2 2
0 2
1 1
5
1 1
9 0
7 10
0 5
2 11
3
5 3
7 2
6 6
4
6 3
9 1
9 6
8 10
0

Output for the Sample Input

4.00 8.00
95.38 39.19
7.00 11.38
27.00 23.63

Rujia Liu's Present 4: A Contest Dedicated to Geometry and CG Lovers

Special Thanks: Dun Liang


题意:给出一些点,你要画出一个矩形把所有点都包在里面,问这样的矩形面积最小是多少,最短的周长是多少。


思路:首先我们做一个凸包,得到一个凸多边形,题目就变成用矩形围住这个凸边形,怎么做呢?可以参考旋转卡壳,我们首先找出minx,maxx,miny,maxy,然后用四条线把这个凸多边形围起来,这个时候围出来的矩形是平行于坐标轴的,我们开始旋转这个矩形的一条边,当然其他边会跟这旋转相同的度数,当其中一条边碰到多边形的一条边时,停止旋转,计算矩形面积和周长,然后继续旋转,直到有一条边旋转的角度超过90度就可以停止了。

这题要注意精度问题,我的eps要到1e-11才能过。。。。


#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string.h>
using namespace std;
#define LL double
#define eps 1e-11

int dcmp(double a, double b)
{
	if (a < b - eps) return -1;
	else if (a > b + eps) return 1;
	else return 0;
}

struct Point
{
	LL x, y;
	Point(LL x = 0, LL y = 0)
		:x(x), y(y) { }
	bool operator<(const Point&p) const
	{
		return dcmp(x,p.x)<0 || (dcmp(x,p.x)==0 && y < p.y);
	}
	bool operator==(const Point&p) const
	{
		return dcmp(x,p.x)==0 && dcmp(y,p.y)==0;
	}
};
typedef Point Vector;

Vector operator+(const Point&a, const Point&b) { return Vector(a.x + b.x, a.y + b.y); }
Vector operator-(const Point&a, const Point&b) { return Vector(a.x - b.x, a.y - b.y); }

LL Dot(const Vector&a, const Vector&b)
{
	return a.x*b.x + a.y*b.y;
}

LL Cross(const Vector&a, const Vector&b)
{
	return a.x*b.y - a.y*b.x;
}

int Convexhull(Point *p, int n, Point*ch)
{
	sort(p, p + n);
	n = unique(p, p + n) - p;
	int m = 0;
	for (int i = 0; i<n; ++i) {
		while (m>1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) --m;
		ch[m++] = p[i];
	}
	int k = m;
	for (int i = n - 2; i >= 0; --i) {
		while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) --m;
		ch[m++] = p[i];
	}
	if (n > 1) --m;
	return m;
}

double Distance(const Point&a, const Point&b)
{
	return sqrt((double)Dot(a - b, a - b));
}

pair<double, double> RotateCalipers(Point * p, int n)
{
	p[n] = p[0];
	int t = 1, l = 1, r = 1;
	double ansC = 1e15, ansA = 1e15;
	for (int b = 0; b<n; ++b) {
		while (Cross(p[b + 1] - p[b], p[t + 1] - p[t])> eps) t = (t + 1) % n;
		while (Dot(p[b + 1] - p[b], p[r + 1] - p[r]) > eps) r = (r + 1) % n;
		if (b == 0) l = (r + 1) % n;
		while (Dot(p[b + 1] - p[b], p[l + 1] - p[l]) < -eps) l = (l + 1) % n;
		double d = Distance(p[b + 1], p[b]);
		double h = (double)Cross(p[b + 1] - p[b], p[t] - p[b]) / d;
		double w = (double)(Dot(p[r] - p[b], p[b + 1] - p[b]) - Dot(p[b + 1] - p[b], p[l] - p[b])) / d;
		ansC = min(ansC, 2 * (h + w));
		ansA = min(ansA, h*w);
	}
	return make_pair(ansA, ansC);
}

const int maxn = 100000 + 5;
int n;
Point pt[maxn];
Point hull[maxn];

void input()
{
	for (int i = 0; i < n; ++i) scanf("%lf%lf", &pt[i].x, &pt[i].y);
}

void solve()
{
	int m = Convexhull(pt, n, hull);
	pair<double, double> ans = RotateCalipers(hull, m);
	printf("%.2lf %.2lf\n", ans.first, ans.second);
}

int main()
{
	while (scanf("%d", &n) == 1)
	{
		if (n == 0) return 0;
		input();
		solve();
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值