题目链接

BZOJ1185

题解

最小矩形一定有一条边在凸包上,枚举这条边,然后旋转卡壳维护另外三个端点即可

计算几何细节极多

  1. 维护另外三个端点尽量不在这条边上,意味着左端点尽量靠后,右端点尽量靠前,加上或减去一个\(eps\)来处理
  2. \(C++\)中\(printf\)输出\(0.00000\)会变成\(-0.00000\),需要特判
  3. 用叉积点乘判距离大小,正负方向不要搞错
  4. 求凸包记得排序
#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]最小矩形覆盖 【旋转卡壳】的更多相关文章

  1. bzoj1185 [HNOI2007]最小矩形覆盖 旋转卡壳求凸包

    [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 2081  Solved: 920 ...

  2. BZOJ 1185: [HNOI2007]最小矩形覆盖 [旋转卡壳]

    1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 1435  Solve ...

  3. 洛谷 P3187 BZOJ 1185 [HNOI2007]最小矩形覆盖 (旋转卡壳)

    题目链接: 洛谷 P3187 [HNOI2007]最小矩形覆盖 BZOJ 1185: [HNOI2007]最小矩形覆盖 Description 给定一些点的坐标,要求求能够覆盖所有点的最小面积的矩形, ...

  4. 【bzoj1185】[HNOI2007]最小矩形覆盖 (旋转卡壳)

    给你一些点,让你用最小的矩形覆盖这些点 首先有一个结论,矩形的一条边一定在凸包上!!! 枚举凸包上的边 用旋转卡壳在凸包上找矩形另外三点... 注意精度问题 #include<cstdio> ...

  5. bzoj 1185 [HNOI2007]最小矩形覆盖——旋转卡壳

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1185 矩形一定贴着凸包的一条边.不过只是感觉这样. 枚举一条边,对面的点就是正常的旋转卡壳. ...

  6. BZOJ 1185: [HNOI2007]最小矩形覆盖-旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标-备忘板子

    来源:旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标 BZOJ又崩了,直接贴一下人家的代码. 代码: #include"stdio.h" #include"str ...

  7. BZOJ1185[HNOI2007] 最小矩形覆盖(旋转卡壳)

    BZOJ1185[HNOI2007] 最小矩形覆盖 题面 给定一些点的坐标,要求求能够覆盖所有点的最小面积的矩形,输出所求矩形的面积和四个顶点的坐标 分析 首先可以先求凸包,因为覆盖了凸包上的顶点,凸 ...

  8. 2018.10.18 bzoj1185: [HNOI2007]最小矩形覆盖(旋转卡壳)

    传送门 不难看出最后的矩形一定有一条边与凸包某条边重合. 因此先求出凸包,然后旋转卡壳求出当前最小矩形面积更新答案. 代码: #include<bits/stdc++.h> #define ...

  9. BZOJ1185 HNOI2007 最小矩形覆盖 凸包、旋转卡壳

    传送门 首先,肯定只有凸包上的点会限制这个矩形,所以建立凸包. 然后可以知道,矩形上一定有一条边与凸包上的边重合,否则可以转一下使得它重合,答案会更小. 于是沿着凸包枚举这一条边,通过旋转卡壳找到离这 ...

随机推荐

  1. [Usaco2005 Open]Disease Manangement 疾病管理 BZOJ1688

    分析: 这个题的状压DP还是比较裸的,考虑将疾病状压,得到DP方程:F[S]为疾病状态为S时的最多奶牛数量,F[S]=max{f[s]+1}; 记得预处理出每个状态下疾病数是多少... 附上代码: # ...

  2. 20155321 《网络攻防》 Exp5 MSF基础应用

    20155321 <网络攻防> Exp5 MSF基础应用 基础问题 用自己的话解释什么是exploit,payload,encode 关于exploit,我觉得exploit是利用一些工具 ...

  3. 【来龙去脉系列】AutoMapper一款自动映射框架

    前言 通常在一个应用程序中,我们开发人员会在两个不同的类型对象之间传输数据,通常我们会用DTOs(数据传输对象),View Models(视图模型),或者直接是一些从一个service或者Web AP ...

  4. ElasticSearch查询 第二篇:文档更新

    <ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...

  5. [穷尽]ADO.NET连接字符串

    微软提供的四种数据库连接方式: System.Data.OleDb.OleDbConnection System.Data.SqlClient.SqlConnection System.Data.Od ...

  6. 设计模式 笔记 观察者模式 Observer

    //---------------------------15/04/27---------------------------- //Observer 观察者模式----对象行为型模式 /* 1:意 ...

  7. 带WIFI模块布局布线要点。

    带WIFI模块布局布线要求: 1: RF底部不能铺铜要挖空不能有GND否则RF信号会被耦合掉从而无法发送出去. 2:WIFI模块下方不能打孔尽量不走线不打孔避开其他信号穿过下方,要整体的铺铜 3:连接 ...

  8. python 游戏(井字棋)

    1. 游戏思路和流程图 实现功能,现实生活中的井字棋玩法 游戏流程图 2. 使用模块和游戏提示 import random def game_info(): print('欢迎来到井字棋游戏') pr ...

  9. Linux内核分析作业第三周

    一.实验楼实验 使用实验楼的虚拟机打开shell 1 cd LinuxKernel/ 2 qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd ...

  10. win7+opencv3.0.0+vs2010 安装及配置

    最近看<学习opencv>,想要跑人脸识别的例子,于是先配环境吧. 1.  opencv下载: 具体下载地址,http://opencv.org/,官网太慢,百度网盘的资源链接:http: ...