BZOJ1185 [HNOI2007]最小矩形覆盖 【旋转卡壳】
题目链接
题解
最小矩形一定有一条边在凸包上,枚举这条边,然后旋转卡壳维护另外三个端点即可
计算几何细节极多
- 维护另外三个端点尽量不在这条边上,意味着左端点尽量靠后,右端点尽量靠前,加上或减去一个\(eps\)来处理
- \(C++\)中\(printf\)输出\(0.00000\)会变成\(-0.00000\),需要特判
- 用叉积点乘判距离大小,正负方向不要搞错
- 求凸包记得排序
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define eps 1e-9
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 50005,maxm = 100005;
const double INF = 1e15;
struct point{double x,y;}p[maxn],G[maxn],t[5];
inline bool operator <(const point& a,const point& b){
return a.x == b.x ? a.y < b.y : a.x < b.x;
}
inline point operator +(const point& a,const point& b){
return (point){a.x + b.x,a.y + b.y};
}
inline point operator -(const point& a,const point& b){
return (point){a.x - b.x,a.y - b.y};
}
inline double operator *(const point& a,const point& b){
return a.x * b.x + a.y * b.y;
}
inline point operator *(const point& a,const double& b){
return (point){a.x * b,a.y * b};
}
inline double cross(const point& a,const point& b){
return a.x * b.y - a.y * b.x;
}
inline double dis(const point& a,const point& b){
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double ans = INF;
int n,st[maxn],top;
void graham(){
sort(p + 1,p + 1 + n);
for (int i = 1; i <= n; i++){
while (top > 1 && cross(p[st[top]] - p[st[top - 1]],p[i] - p[st[top]]) >= 0) top--;
st[++top] = i;
}
int tmp = top;
for (int i = n - 1; i; i--){
while (top > tmp && cross(p[st[top]] - p[st[top - 1]],p[i] - p[st[top]]) >= 0) top--;
st[++top] = i;
}
top--;
REP(i,top) G[i - 1] = p[st[i]];
G[top] = G[0];
}
void work(){
int d,l,r; d = l = r = 1;
double L,R,D,H;
for (int i = 0; i < top; i++){
D = dis(G[i],G[i + 1]);
while (cross(G[d + 1] - G[i],G[i + 1] - G[i]) + eps >= cross(G[d] - G[i],G[i + 1] - G[i])) d = (d + 1) % top;
while ((G[i + 1] - G[i]) * (G[l + 1] - G[i + 1]) + eps >= (G[i + 1] - G[i]) * (G[l] - G[i + 1])) l = (l + 1) % top;
if (i == 0) r = d;
while ((G[i] - G[i + 1]) * (G[r + 1] - G[i]) - eps > (G[i] - G[i + 1]) * (G[r] - G[i])) r = (r + 1) % top;
H = fabs(cross(G[d] - G[i],G[i + 1] - G[i]) / D);
L = fabs((G[i + 1] - G[i]) * (G[l] - G[i + 1]) / D);
R = fabs((G[i + 1] - G[i]) * (G[r] - G[i]) / D);
//printf("(%.0lf,%.0lf) (%.0lf,%.0lf) ",G[i].x,G[i].y,G[i + 1].x,G[i + 1].y);
//printf("(%.0lf,%.0lf) (%.0lf,%.0lf) (%.0lf,%.0lf)",G[l].x,G[l].y,G[d].x,G[d].y,G[r].x,G[r].y);
//printf("D = %lf H = %lf\n",L + R + D,H);
double S = (L + R + D) * H;
if (S < ans){
ans = S;
t[0] = G[i + 1] + (G[i + 1] - G[i]) * (L / D);
t[1] = G[i] + (G[i] - G[i + 1]) * (R / D);
t[2] = t[1] + (G[r] - t[1]) * (H / dis(G[r],t[1]));
t[3] = t[0] + (G[l] - t[0]) * (H / dis(G[l],t[0]));
}
}
}
int main(){
scanf("%d",&n);
REP(i,n) scanf("%lf%lf",&p[i].x,&p[i].y);
graham();
//for (int i = 0; i <= top; i++) printf("(%lf,%lf)\n",G[i].x,G[i].y);
work();
if (fabs(ans) < eps) puts("0.00000");
else printf("%.5lf\n",ans);
int pos = 0;
for (int i = 1; i < 4; i++)
if (t[i].y < t[pos].y) pos = i;
for (int i = pos,j = 0; j < 4; i = (i + 1) % 4,j++){
if (fabs(t[i].x) < eps) printf("0.00000 ");
else printf("%.5lf ",t[i].x);
if (fabs(t[i].y) < eps) printf("0.00000\n");
else printf("%.5lf\n",t[i].y);
}
return 0;
}
BZOJ1185 [HNOI2007]最小矩形覆盖 【旋转卡壳】的更多相关文章
- bzoj1185 [HNOI2007]最小矩形覆盖 旋转卡壳求凸包
[HNOI2007]最小矩形覆盖 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 2081 Solved: 920 ...
- BZOJ 1185: [HNOI2007]最小矩形覆盖 [旋转卡壳]
1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 1435 Solve ...
- 洛谷 P3187 BZOJ 1185 [HNOI2007]最小矩形覆盖 (旋转卡壳)
题目链接: 洛谷 P3187 [HNOI2007]最小矩形覆盖 BZOJ 1185: [HNOI2007]最小矩形覆盖 Description 给定一些点的坐标,要求求能够覆盖所有点的最小面积的矩形, ...
- 【bzoj1185】[HNOI2007]最小矩形覆盖 (旋转卡壳)
给你一些点,让你用最小的矩形覆盖这些点 首先有一个结论,矩形的一条边一定在凸包上!!! 枚举凸包上的边 用旋转卡壳在凸包上找矩形另外三点... 注意精度问题 #include<cstdio> ...
- bzoj 1185 [HNOI2007]最小矩形覆盖——旋转卡壳
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1185 矩形一定贴着凸包的一条边.不过只是感觉这样. 枚举一条边,对面的点就是正常的旋转卡壳. ...
- BZOJ 1185: [HNOI2007]最小矩形覆盖-旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标-备忘板子
来源:旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标 BZOJ又崩了,直接贴一下人家的代码. 代码: #include"stdio.h" #include"str ...
- BZOJ1185[HNOI2007] 最小矩形覆盖(旋转卡壳)
BZOJ1185[HNOI2007] 最小矩形覆盖 题面 给定一些点的坐标,要求求能够覆盖所有点的最小面积的矩形,输出所求矩形的面积和四个顶点的坐标 分析 首先可以先求凸包,因为覆盖了凸包上的顶点,凸 ...
- 2018.10.18 bzoj1185: [HNOI2007]最小矩形覆盖(旋转卡壳)
传送门 不难看出最后的矩形一定有一条边与凸包某条边重合. 因此先求出凸包,然后旋转卡壳求出当前最小矩形面积更新答案. 代码: #include<bits/stdc++.h> #define ...
- BZOJ1185 HNOI2007 最小矩形覆盖 凸包、旋转卡壳
传送门 首先,肯定只有凸包上的点会限制这个矩形,所以建立凸包. 然后可以知道,矩形上一定有一条边与凸包上的边重合,否则可以转一下使得它重合,答案会更小. 于是沿着凸包枚举这一条边,通过旋转卡壳找到离这 ...
随机推荐
- centos7 sentry部署指南
依赖说明 sentry官方推荐docker方式安装,使用到了docker-compose.docker至少是1.10.3以上的版本.为此需要使用centos7. 安装docker #添加yum 源 # ...
- 20155217《网络对抗》Exp06 信息搜集与漏洞扫描
20155217<网络对抗>Exp06 信息搜集与漏洞扫描 实践内容 各种搜索技巧的应用 DNS IP注册信息的查询 基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务的查点 ...
- mfc CIPAddressCtrl控件
知识点: CIPAddressCtrl 属性 CIPAddressCtrl 成员函数 成员函数代码测试 一.CIPAddressCtrl Class Members IsBlank Determine ...
- python语言程序设计2
1, 代码高亮色彩体系 2, 缩进,一行代码开始前的空白区域,表达程序的格式框架 单层缩进,多层缩进 特点 概念,缩进是语法的一部分,缩进不正确的话可能会导致程序运行错误 用处(意义),是表达代码间包 ...
- 记一次Java加密加签算法到php的坑
此文为本人原创首发于 http://www.35coder.com/convert_encryption_codes_to_php/. 写代码的经历中,总少不了与外部的程序对接,一旦有这样的事,往往周 ...
- BCompare破解方法
1.删除 BCUnrar.dll 文件,重启软件. 备注:使用everything搜索BCUnrar.dll
- pandas 初识(四)
Pandas 和 sqlalchemy 配合实现分页查询 Mysql 并获取总条数 @api.route('/show', methods=["POST"]) def api_sh ...
- npm install的几种命令形式区别
转自未来与传说.jigetage 我们在使用 npm install 安装模块的时候 ,一般会使用下面这几种命令形式: npm install moduleName # 安装模块到项目目录下 npm ...
- 树形DP ---- Codeforces Global Round 2 F. Niyaz and Small Degrees引发的一场血案
Aspirations:没有结果,没有成绩,acm是否有意义?它最大的意义就是让我培养快速理解和应用一个个未知知识点的能力. ————————————————————————————————————— ...
- 微软职位内部推荐-Senior Software Engineer-Eco
微软近期Open的职位: The MOD Ecosystem team is dedicated to expanding the reach and value of Office by enabl ...