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 ...
随机推荐
- AWT初步—Frame和 Panel
初识 AWT GUI 和 AWT GUI:Graphics User Interface 图形用户界面 AWT:Abstract Window Toolkit 抽象窗口工具集 之前的程 ...
- easyUI combobox combotree 模糊查询,带上下键选择功能,待完善。。。。
/2017年4月9日 11:52:36 /** * combobox和combotree模糊查询 * combotree 结果带两级父节点(手动设置数量) * 键盘上下键选择叶子节点 * 键盘回车键设 ...
- 【PHP调试篇】PHP高性能日志组件SeasLog
简述 什么是SeasLog SeasLog是一个C语言编写的PHP扩展,提供一组规范标准的功能函数,在PHP项目中方便.规范.高效地写日志,以及快速地读取和查询日志. 为什么使用SeasLog 无论在 ...
- python第五十七天------补上笔记
direct_client:广播接收 #!/usr/bin/env python #_*_coding:utf-8_*_ import pika,time,sys connection = pika. ...
- iOS 多线程之GCD的简单使用
在iOS开发中,遇到耗时操作,我们经常用到多线程技术.Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法,只需定义想要执行的任务,然后添加到适当的调度队列 ...
- AIX mount nfs 文件系统失败
报 mount: 1831-008 的错,配置系统参数后恢复. 操作系统版本为: # oslevel 6.1.0.0 LOG如下: # mount 192.168.240.69:/xyz/xvdh2/ ...
- 联想笔记本Y7000P安装nvidia,cuda,tensorflow,pytorch
Y7000P电脑环境i7处理器,1060显卡,16g内存,win10家庭版(系统版本号1809),在联想官网升级过bios,所有驱动都是最新.(截止时间点2019年3月1日) python3.5 安装 ...
- React路由 + 绝对路径引用
路由: 哈希路由(在url地址后加 #name) // 实现页面监听 window.onhashchange = function(){ console.log(‘hash:’,window.lo ...
- 个人技术博客(α)------javaweb的学习路程
该博文大致内容是学习的一个过程,心得,并不是以技术博客为主,在此说明. 关于javaweb的学习开始的时间大概是从大二下(2017年6.7月份)的暑假开始的,在学长的介绍下加入了实验室进行学习,由于是 ...
- (14)Python类