BZOJ1185 HNOI2007 最小矩形覆盖 凸包、旋转卡壳
首先,肯定只有凸包上的点会限制这个矩形,所以建立凸包。
然后可以知道,矩形上一定有一条边与凸包上的边重合,否则可以转一下使得它重合,答案会更小。
于是沿着凸包枚举这一条边,通过旋转卡壳找到离这条边最远的点以及这个矩形两端的点,这五个点构成的矩形就是一个可能的答案了。
各种判断用向量叉积和点积
注意一下输出\(-0.0000\)的情况
#include<bits/stdc++.h>
#define ld long double
#define eps 1e-8
//This code is written by Itst
using namespace std;
const int MAXN = 5e4 + 10;
struct vec{
ld x , y;
vec(ld _x = 0 , ld _y = 0){x = _x; y = _y;}
bool operator <(const vec a)const{
return x < a.x;
}
vec operator -(vec a){
return vec(x - a.x , y - a.y);
}
vec operator *(ld p){
return vec(p * x , p * y);
}
vec operator +(vec a){
return vec(x + a.x , y + a.y);
}
}now[MAXN] , temp[MAXN] , squ[4];
int cnt , N , top , st[MAXN] , ind[MAXN];
ld ans;
inline bool cmp(ld a , ld b){
return a - eps < b && a + eps > b;
}
inline ld cot(vec a , vec b){
return a.x * b.y - a.y * b.x;
}
inline ld dot(vec a , vec b){
return a.x * b.x + a.y * b.y;
}
inline ld calS(vec a , vec b , vec c){
return fabs(cot(c - a , b - a));
}
inline ld len(vec a){
return sqrt(a.x * a.x + a.y * a.y);
}
inline ld calS(vec a , vec b , vec c , vec d , vec e){
return calS(a , d , b) / len(b - a) * dot(c - e , b - a) / len(b - a);
}
inline vec rev(vec a){
return vec(-a.y , a.x);
}
void input(){
cin >> N;
for(int i = 1 ; i <= N ; ++i)
cin >> now[i].x >> now[i].y;
}
void init(){
ans = 1e18;
sort(now + 1 , now + N + 1);
for(int i = 1 ; i <= N ; ++i){
while(top >= 2 && cot(now[i] - now[st[top - 1]] , now[st[top]] - now[st[top - 1]]) > -eps)
--top;
st[++top] = i;
}
for(int i = 1 ; i <= top ; ++i)
ind[++cnt] = st[i];
top = 0;
for(int i = N ; i ; --i){
while(top >= 2 && cot(now[i] - now[st[top - 1]] , now[st[top]] - now[st[top - 1]]) > -eps)
--top;
st[++top] = i;
}
for(int i = 2 ; i < top ; ++i)
ind[++cnt] = st[i];
for(int i = 1 ; i <= cnt ; ++i)
temp[i] = now[ind[i]];
memcpy(now + 1 , temp + 1 , sizeof(vec) * cnt);
}
void work(){
int minX = 1 , maxX = 1 , minY = 1 , maxY = 1;
for(int i = 2 ; i <= N ; ++i){
if(now[minX].x > now[i].x)
minX = i;
if(now[maxX].x < now[i].x)
maxX = i;
if(now[minY].y > now[i].y)
minY = i;
if(now[maxY].y < now[i].y)
maxY = i;
}
ans = (now[maxY].y - now[minY].y) * (now[maxX].x - now[minX].x);
squ[0].x = squ[3].x = now[minX].x;
squ[1].x = squ[2].x = now[maxX].x;
squ[0].y = squ[1].y = now[minY].y;
squ[2].y = squ[3].y = now[maxY].y;
for(int i = 1 ; i <= cnt ; minY = minY % cnt + 1 , ++i){
while(calS(now[minY] , now[maxY] , now[minY % cnt + 1]) < calS(now[minY] , now[maxY % cnt + 1] , now[minY % cnt + 1]))
maxY = maxY % cnt + 1;
while(dot(now[minY % cnt + 1] - now[minY] , now[minX % cnt + 1] - now[minX]) < eps)
minX = minX % cnt + 1;
while(dot(now[minY % cnt + 1] - now[minY] , now[maxX % cnt + 1] - now[maxX]) > -eps)
maxX = maxX % cnt + 1;
ld t = calS(now[minY] , now[minY % cnt + 1] , now[maxX] , now[maxY] , now[minX]);
if(t < ans){
ans = t;
squ[0] = (now[minY % cnt + 1] - now[minY]) * (dot(now[minY] - now[maxX] , now[minY] - now[minY % cnt + 1]) / len(now[minY] - now[minY % cnt + 1]) / len(now[minY] - now[minY % cnt + 1])) + now[minY];
squ[1] = rev(now[minY % cnt + 1] - now[minY]) * (calS(now[minY] , now[minY % cnt + 1] , now[maxY]) / len(now[minY] - now[minY % cnt + 1]) / len(now[minY] - now[minY % cnt + 1])) + squ[0];
squ[2] = (now[minY] - now[minY % cnt + 1]) * (t / len(squ[1] - squ[0]) / len(now[minY % cnt + 1] - now[minY])) + squ[1];
squ[3] = squ[2] + (squ[0] - squ[1]);
}
}
}
void output(){
cout << fixed << setprecision(5) << (ans < 1e-5 ? 0 : ans) << endl;
int dir = 0;
for(int j = 1 ; j < 4 ; ++j)
if(squ[dir].y > squ[j].y || cmp(squ[dir].y , squ[j].y) && squ[j].x < squ[dir].x)
dir = j;
for(int i = 0 ; i < 4 ; ++i)
cout << fixed << setprecision(5) << (squ[(dir + i) % 4].x < 1e-5 ? 0 : squ[(dir + i) % 4].x) << ' ' << (squ[(dir + i) % 4].y < 1e-5 ? 0 : squ[(dir + i) % 4].y) << endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in" , "r" , stdin);
//freopen("out" , "w" , stdout);
#endif
input();
init();
work();
output();
return 0;
}
BZOJ1185 HNOI2007 最小矩形覆盖 凸包、旋转卡壳的更多相关文章
- [BZOJ1185][HNOI2007]最小矩形覆盖-[凸包+旋转卡壳]
Description 传送门 Solution 感性理解一下,最小矩形一定是由一条边和凸包上的边重合的. 然后它就是模板题了..然而真的好难调,小于大于动不动就打错. Code #include&l ...
- BZOJ1185[HNOI2007] 最小矩形覆盖(旋转卡壳)
BZOJ1185[HNOI2007] 最小矩形覆盖 题面 给定一些点的坐标,要求求能够覆盖所有点的最小面积的矩形,输出所求矩形的面积和四个顶点的坐标 分析 首先可以先求凸包,因为覆盖了凸包上的顶点,凸 ...
- BZOJ1185 [HNOI2007]最小矩形覆盖 【旋转卡壳】
题目链接 BZOJ1185 题解 最小矩形一定有一条边在凸包上,枚举这条边,然后旋转卡壳维护另外三个端点即可 计算几何细节极多 维护另外三个端点尽量不在这条边上,意味着左端点尽量靠后,右端点尽量靠前, ...
- bzoj 1185 [HNOI2007]最小矩形覆盖 凸包+旋转卡壳
题目大意 用最小矩形覆盖平面上所有的点 分析 有一结论:最小矩形中有一条边在凸包的边上,不然可以旋转一个角度让面积变小 简略证明 我们逆时针枚举一条边 用旋转卡壳维护此时最左,最右,最上的点 注意 注 ...
- 2018.10.18 bzoj1185: [HNOI2007]最小矩形覆盖(旋转卡壳)
传送门 不难看出最后的矩形一定有一条边与凸包某条边重合. 因此先求出凸包,然后旋转卡壳求出当前最小矩形面积更新答案. 代码: #include<bits/stdc++.h> #define ...
- [HNOI2007][BZOJ1185] 最小矩形覆盖 [凸包+旋转卡壳]
题面 BZOJ题面 前置芝士 建议先学习向量相关的计算几何基础 计算几何基础戳这里 思路 用这道题学习一下凸包和旋转卡壳 首先是凸包部分 凸包 求凸包用的算法是graham算法 算法流程如下: 找到$ ...
- bzoj1185 [HNOI2007]最小矩形覆盖 旋转卡壳求凸包
[HNOI2007]最小矩形覆盖 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 2081 Solved: 920 ...
- BZOJ1185 : [HNOI2007]最小矩形覆盖
求出凸包后,矩形的一条边一定与凸包的某条边重合. 枚举每条边,求出离它最远的点和离它最左最右的点,因为那三个点是单调变化的,所以复杂度为$O(n)$. 注意精度. #include<cstdio ...
- bzoj千题计划209:bzoj1185: [HNOI2007]最小矩形覆盖
http://www.lydsy.com/JudgeOnline/problem.php?id=1185 题解去看它 http://www.cnblogs.com/TheRoadToTheGold/p ...
随机推荐
- 消除TortoiseSVN 检出到(checkout)桌面上显示一堆问号
之前不小心直接将版本库的内容检出到桌面,后才发现桌面上的文件图标都变成了问号,新建文件夹也同样如此. 为了解决这个问题,采用如下方法(任何一个检出文件夹均可这样操作): 1.删除桌面隐藏的.SVN文件 ...
- 洗礼灵魂,修炼python(47)--巩固篇—定义类的方法之@classmethod,@staticmethod
定义类的方法,相信你会说,不就是在class语句下使用def () 就是定义类的方法了嘛,是的,这是定义的方法的一种,而且是最普通的方式 首先,我们已经知道有两种方式: 1.普通方法: 1)与类无关的 ...
- EF扩展 更新指定字段
using System.Data.Entity.Infrastructure; using System.Threading.Tasks; /// <summary> /// EF扩展 ...
- java调用python的惨痛史(无法获取环境变量)
环境:java,was,python2.6,红帽linux,oracle,python用cx_Oracle 事情是这样的,有个需求,需要对数据库进行处理,简单说就是把数据取出来,用pyt ...
- 第六章 键盘(SYSMETS4)
//SYSMETS.H -- System metrics display structure #include <Windows.h> #define NUMLINES ((int) ( ...
- 【PAT】A1001A+B Format
最新想法: 最多是七位数,而且只有一组输入,完全不用考虑算法复杂度. 直接判断是否为负,并输出符号 巧妙的地方:while循环的下一次再添加逗号.(防止出现,999,991的情况) 婼姐的方法真的很巧 ...
- bash的内置字符串处理工具
bash内置字符串处理工具: 1.字符串切片: ${var:offset:number} 取字符串的子串: ${var: -length} 取字符串的最右侧的length个字符.注意:冒号右侧必须有 ...
- 基于C#的单元测试(VS2015)
这次来联系怎么用VS2015来进行C#代码的单元测试管理,首先,正好上次写了一个C#的WordCount程序,就用它来进行单元测试联系吧. 首先,根据VS2015的提示,仅支持在共有类或共有方法中支持 ...
- RBAC权限管理系统数据模型
懒得多写了,懂的看建表脚本就懂了... -- ---------------------------- -- Table structure for ucb_user -- ------------- ...
- [项目实践] python文件路径引用的规则,记一次使用sys.path[0]的问题,及如何区分 ../与 ./的使用场景
下面是一个获取配置的代码 def getValue(self,section,option): """ @file: string,the name of the con ...