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 ...
随机推荐
- 【读书笔记】iOS-“一心多用”利用多线程提升性能
iPhone将具有支持不同类型多线程API的能力,这些API包括:POSIX线程,NSObject,NSThread和NSOperation. iPhone操作系统是一个真正的抢占式,多任务操作系统, ...
- CSS3圆圈动画放大缩小循环动画效果
代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- maven 技巧
M2Eclipse Releases maven eclipse插件最新安装地址 Full Version Tag 1.0 2011-06-22 http://download.eclipse.org ...
- GridView的簡單使用
項目GitHub地址:https://github.com/leonInShanghai/IMbobo GridView XML佈局: <?xml version="1.0" ...
- macos 下安装brew
1.终端执行 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master ...
- [20171120]11G关闭直接路径读.txt
[20171120]11G关闭直接路径读.txt --//今天做filesystemio_options参数测试时,遇到一个关于直接路径读的问题.--//如果看以前的博客介绍,设置"_ser ...
- Vue延迟点击
从点击屏幕上的元素到触发元素的 click 事件,移动浏览器会有大约 300 毫秒的等待时间 fastclick清除点击延迟,让程序更灵敏 1.导出fastclick import Vue from ...
- 使用缓存方式优化递归函数与lru_cache
一.递归函数的弊端 递归函数虽然编写时用很少的代码完成了庞大的功能,但是它的弊端确实非常明显的,那就是时间与空间的消耗. 用一个斐波那契数列来举例 import time #@lru_cache(20 ...
- 第五章 绘图基础(SINEWAVE)
//SINEWAVE.C -- Sine Wave Using Polyline (c) Charles Petzold, 1998 #include <Windows.h> #inclu ...
- python redirect和render的区别
render是渲染变量到模板中,而redirect是HTTP中1个跳转的函数,一般会生成302状态码.