UVA10173 Smallest Bounding Rectangle 最小面积矩形覆盖
\(\color{#0066ff}{题目描述}\)
给定n(>0)二维点的笛卡尔坐标,编写一个程序,计算其最小边界矩形的面积(包含所有给定点的最小矩形)。
输入文件可以包含多个测试样例。每个测试样例从包含一个正数的行开始。 整数N(<1001),表示该测试样例中的点的数量。接下来的n行各包含两个实数,分别给出一个点的x和y坐标。输入最后包含一个值为0的测试样例,该值必须不被处理。
对于输入中的每个测试样例都输出一行,包含最小边界矩形的面积,小数点后四舍五入到第四位。
\(\color{#0066ff}{输入格式}\)
\(\color{#0066ff}{输出格式}\)
\(\color{#0066ff}{输入样例}\)
3
-3.000 5.000
7.000 9.000
17.000 5.000
4
10.000 10.000
10.000 20.000
20.000 20.000
20.000 10.000
0
\(\color{#0066ff}{输出样例}\)
80.0000
100.0000
\(\color{#0066ff}{数据范围与提示}\)
none
\(\color{#0066ff}{题解}\)
旋转卡壳
首先先求一遍凸包
对于最小面积外接矩形,显然至少有一条边与凸包重合,就枚举那条边(底边)
对于上面的边,通过叉积叉出面积判断最高位置,找到上边
对于左边,用点积,找到最大点积(投影最长,那么最靠左),右边指针从左边开始找投影最短
每次移动底边,然后更新上左右,来更新ans
#include <bits/stdc++.h>
#define _ 0
#define LL long long
inline LL in() {
LL x = 0, f = 1; char ch;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
return x * f;
}
struct node {
double x,y;
node(double x = 0, double y = 0):x(x), y(y) {}
node operator - (const node &b) const {
return node(x - b.x, y - b.y);
}
double operator ^ (const node &b) const {
return x * b.y - y * b.x;
}
double operator * (const node &b) const {
return x * b.x + y * b.y;
}
double mo() {
return sqrt(x * x + y * y);
}
double jj() const {
return atan2(y, x);
}
};
const int maxn = 10005;
node e[maxn], v[maxn];
int n,top;
double S(node a,node b,node c) {
return (b - a) ^ (c - a);
}
bool cmp(const node &a, const node &b) {
return (a.jj() < b.jj() || (a.jj() == b.jj() && (a - e[0]).mo() < (b - e[0]).mo()));
}
void tubao() {
int min = 0;
for(int i = 0; i < n; i++)
if(e[i].y < e[min].y || (e[i].y == e[min].y && e[i].x < e[min].x)) min = i;
std::swap(e[0], e[min]);
for(int i = 1; i < n; i++) e[i] = e[i] - e[0];
e[0] = 0;
std::sort(e + 1, e + n, cmp);
v[0] = e[0], v[1] = e[1];
for(int i = top = 2; i < n; i++) {
while((top > 1) && (S(v[top - 2], v[top - 1], e[i]) <= 0)) top--;
v[top++] = e[i];
}
v[top] = e[0];
}
double C(node a,node b,node c) {
return (c - a) * (b - a);
}
double rotate()
{
if(top < 3) return 0;
int l = 1, u = 1, r;
double a, b, c, ans = 1e20;
for(int i = 0; i < top; i++) {
while(S(v[i], v[i + 1], v[u + 1]) > S(v[i], v[i + 1], v[u])) u = (u + 1) % top;
while(C(v[i], v[i + 1], v[l + 1]) > C(v[i], v[i + 1], v[l])) l = (l + 1) % top;
if(!i) r = l;
while(C(v[i], v[i + 1], v[r + 1]) <= C(v[i], v[i + 1], v[r])) r = (r + 1) % top;
a = S(v[i], v[i + 1], v[u]);
b = C(v[i], v[i + 1], v[l]) - C(v[i], v[i + 1], v[r]);
c = C(v[i], v[i + 1], v[i + 1]);
ans = std::min(ans, a * b / c);
}
return ans;
}
int main() {
while("fuck") {
n = in();
if(!n) break;
for(int i = 0; i < n; i++) scanf("%lf%lf", &e[i].x, &e[i].y);
tubao();
printf("%.4lf\n", rotate());
}
return 0;
}
UVA10173 Smallest Bounding Rectangle 最小面积矩形覆盖的更多相关文章
- LeetCode 939. Minimum Area Rectangle (最小面积矩形)
题目标签:HashMap 题目给了我们一组 xy 上的点坐标,让我们找出 能组成矩形里最小面积的那个. 首先遍历所有的点,把x 坐标当作key 存入map, 把重复的y坐标 组成set,当作value ...
- Smallest Bounding Rectangle - uva10173
Smallest Bounding Rectangle Given the Cartesian coordinates of n(>0)2-dimensional points, write a ...
- 此坑待填 离散化思想和凸包 UVA - 10173 Smallest Bounding Rectangle
Smallest Bounding Rectangle Given the Cartesian coordinates of n(>0)2-dimensional points, write a ...
- LeetCode939 最小面积矩形
LeetCode939最小面积矩形 给定在 xy 平面上的一组点,确定由这些点组成的矩形的最小面积,其中矩形的边平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. Input [[1,1],[ ...
- [Swift]LeetCode939. 最小面积矩形 | Minimum Area Rectangle
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these p ...
- [Swift]LeetCode963. 最小面积矩形 II | Minimum Area Rectangle II
Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...
- Leetcode963. Minimum Area Rectangle II最小面积矩形2
给定在 xy 平面上的一组点,确定由这些点组成的任何矩形的最小面积,其中矩形的边不一定平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. 示例 1: 输入:[[1,2],[2,1],[1,0] ...
- bzoj 1185 旋转卡壳 最小矩形覆盖
题目大意 就是求一个最小矩形覆盖,逆时针输出其上面的点 这里可以看出,那个最小的矩形覆盖必然有一条边经过其中凸包上的两个点,另外三条边必然至少经过其中一个点,而这样的每一个点逆时针走一遍都满足单调性 ...
- hdu5251最小矩形覆盖
题意(中问题直接粘吧)矩形面积 Problem Description 小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些矩形包围起来的面积最小的矩形的面积是多少. Input ...
随机推荐
- 12-15winform--窗体
一.窗体:有许多控件组成,空间本身就是类对象: 1)每一个控件都有自己的属性和方法. 2)控件的方法叫做事件: 3)同一命名空间下的项目文件是一个整体文件.设计器的代码(类文件)在“解决方案管理器”- ...
- ASP.NET 连接MySql数据库
ASP.NET Mysql操作类 以下连接MySql数据库以VS2010为例,对于其他的编辑器也差不多 1. 我们需要在Mysql官网下载一个组件http://dev.mysql.com/downlo ...
- ListView显示Sqlite的数据
在安卓中,ListView和Sqlite都是十分常用的.这次我们来结合这个两个知识点写一个Demo. 功能:吧SQLite中的数据用ListView显示出来. 先看截图吧 首先是数据库 然后是运行截图 ...
- Access restriction required library rt.jar
在JAVA项目开发中,使用到了BASE64Decoder,但编辑运行时却会出现以下错误:Access restriction required library rt.jar,这里就详细的说明一下如何解 ...
- dp-最小点对问题
dp-最小点对问题 //最小点对问题 //采用分治思想,先分成两个子集分别求出最短距离d //再对两个子集进行合并,在一个dx2d的矩形中,最多可能有6个点距离小于d //按y排序,当x增长时求出这6 ...
- spring 的aop操作
- spring----AOP注解以及spring的JDBC和事务
技术分析之:Spring框架的AOP技术(注解方式) 1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开 ...
- 关于c#数据类型,类型转换,变量,常量,转义符。。。
先说一下数据类型...数据类型可以分为两大类:基本数据类型和引用类型. 基本数据类型按功能又分为“值类型”,“布尔型”,“字符型”. 引用类型分为“字符串”,“时间日期”. 没图没真相↓面放图. 橙 ...
- 25-Fibonacci(矩阵快速幂)
http://poj.org/problem?id=3070 Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- R: 自动计算代码运行时间
################################################### 问题:代码运行时间 18.4.25 怎么计算代码的运行时间? 解决方案: ptm = pro ...