UVA 12307 Smallest Enclosing Rectangle(旋转卡壳)
题意:给你一些点,找出两个可以包含所有点的矩形,一个保证矩形面积最小,一个保证矩形周长最小,输出两个最小值
题解:首先根据所有点求一个凸包,再在这个凸包上枚举每条边,作为矩形的一条边(这样可以保证最小)
接着根据旋转卡壳的思想求出另外三条边,这样枚举判断就好
求另三条边时首先方向是确定了的,找点就是旋转卡壳,思想就是:枚举的任意两条边a与b,a的另三条边与b的另三条边都不会再a与b之间,并且b对应边一定最a对应边的 后面(注意是循环的边)那么就是说,我们可以使用类似双指针方式维护,但是时间复杂度却为O(n)
- #include<set>
- #include<map>
- #include<queue>
- #include<stack>
- #include<cmath>
- #include<vector>
- #include<string>
- #include<cstdio>
- #include<cstring>
- #include<iomanip>
- #include<stdlib.h>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- #define eps 1E-8
- /*注意可能会有输出-0.000*/
- #define sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
- #define cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
- #define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
- #define mul(a,b) (a<<b)
- #define dir(a,b) (a>>b)
- typedef long long ll;
- typedef unsigned long long ull;
- const int Inf=<<;
- const ll INF=1LL<<;
- const double Pi=acos(-1.0);
- const int Mod=1e9+;
- const int Max=;
- struct Point
- {
- double x,y;
- Point(double x=,double y=):x(x),y(y) {};
- inline Point operator-(const Point& a)const
- {
- return Point(x-a.x,y-a.y);
- }
- inline bool operator<(const Point& a)const
- {
- return sgn(x-a.x)<||zero(x-a.x)&&sgn(y-a.y)<;
- }
- inline Point operator+(const Point& a)const
- {
- return Point(x+a.x,y+a.y);
- }
- inline bool operator!=(const Point& a)const
- {
- return !(zero(x-a.x)&&zero(y-a.y));
- }
- };
- typedef Point Vector;
- struct Line
- {
- Point p;
- Vector v;
- double ang;//极角
- Line() {};
- Line(Point p,Vector v):p(p),v(v)
- {
- ang=atan2(v.y,v.x);
- }
- inline bool operator<(const Line& L)const
- {
- return ang<L.ang;
- }
- };
- double Dis(Point A,Point B)
- {
- return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
- }
- double Cross(Vector A,Vector B)
- {
- return A.x*B.y-A.y*B.x;
- }
- int ConvexHull(Point *p,int n,Point *convex)//求凸包
- {
- sort(p,p+n);
- int m=;
- for(int i=; i<n; ++i)
- {
- while(m>&&Cross(convex[m-]-convex[m-],p[i]-convex[m-])<)
- {
- m--;
- }
- convex[m++]=p[i];
- }
- int k=m;
- for(int i=n-; i>=; --i)
- {
- while(m>&&Cross(convex[m-]-convex[m-],p[i]-convex[m-])<)
- {
- m--;
- }
- convex[m++]=p[i];
- }
- if(n>)
- m--;
- return m;
- }
- Point intersection(Point p1,Point p2,Point l1,Point l2)//交点坐标
- {
- Point ret=p1;//首先计算直线是否平行
- double t=((p1.x-l1.x)*(l1.y-l2.y)-(p1.y-l1.y)*(l1.x-l2.x))
- /((p1.x-p2.x)*(l1.y-l2.y)-(p1.y-p2.y)*(l1.x-l2.x));
- ret.x+=(p2.x-p1.x)*t;
- ret.y+=(p2.y-p1.y)*t;
- return ret;//线段交点另外判断线段相交(同时判断是否平行)
- }
- Point now[Max],convex[Max];
- double area,per;
- double GetArea(Line up,Line down,Line left,Line right)//根据矩形四条线求面积
- {
- Point minx=intersection(left.p,left.p+left.v,down.p,down.p+down.v);
- Point miny=intersection(right.p,right.p+right.v,down.p,down.p+down.v);
- Point manx=intersection(left.p,left.p+left.v,up.p,up.p+up.v);
- return Dis(minx,manx)*Dis(minx,miny);
- }
- double GetPer(Line up,Line down,Line left,Line right)
- {
- Point minx=intersection(left.p,left.p+left.v,down.p,down.p+down.v);
- Point miny=intersection(right.p,right.p+right.v,down.p,down.p+down.v);
- Point manx=intersection(left.p,left.p+left.v,up.p,up.p+up.v);
- return (Dis(minx,manx)+Dis(minx,miny))*;
- }
- Vector Rotate(Vector A,double rad) //向量A逆时针旋转rad
- {
- return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
- }
- void RotateStuck(int n)//旋转卡壳枚举矩形
- {
- for(int i=;i<n;++i)
- {
- convex[i+n]=convex[i];
- convex[i+n+n]=convex[i];
- }
- area=per=Inf;
- Line up,down,left,right;//四条直线
- int i=,j=,k=,l=;//四条线的位置
- for(; i<n; ++i)//枚举上这条线,则可以确定其他
- {
- up=Line(convex[i],convex[i+]-convex[i]);//其他三条直线所在的点与上这条线成单峰函数
- k=max(i,k);//每次是逆时针旋转,保证是凸包上一条线或者后面的线
- while(Cross(Rotate(up.v,Pi/),convex[k+]-convex[k])<)//通过旋转来判断
- k++;
- left=Line(convex[k],Rotate(up.v,Pi/));
- j=max(k,j);
- while(Cross(Rotate(up.v,Pi),convex[j+]-convex[j])<)
- j++;
- down=Line(convex[j],Rotate(up.v,Pi));
- l=max(j,l);
- while(Cross(Rotate(up.v,*Pi/),convex[l+]-convex[l])<)
- l++;
- right=Line(convex[l],Rotate(up.v,*Pi/));
- area=min(area,GetArea(up,down,left,right));
- per=min(per,GetPer(up,down,left,right));
- }
- return ;
- }
- int main()
- {
- int n;
- while(~scanf("%d",&n)&&n)
- {
- for(int i=; i<n; ++i)
- {
- scanf("%lf %lf",&now[i].x,&now[i].y);
- }
- int m= ConvexHull(now,n,convex);
- RotateStuck(m);
- printf("%.2f %.2f\n",area,per);
- }
- return ;
- }
UVA 12307 Smallest Enclosing Rectangle(旋转卡壳)的更多相关文章
- UVA 12307 Smallest Enclosing Rectangle
https://vjudge.net/problem/UVA-12307 求覆盖所有点的最小矩形面积.周长 相当于求凸包的最小面积外接矩形.最小周长外接矩形 结论: 这个矩形一定有一条边和凸包上一条边 ...
- 此坑待填 离散化思想和凸包 UVA - 10173 Smallest Bounding Rectangle
Smallest Bounding Rectangle Given the Cartesian coordinates of n(>0)2-dimensional points, write a ...
- UVA 4728 Squares(凸包+旋转卡壳)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...
- UVa 1453 - Squares 旋转卡壳求凸包直径
旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...
- UVa1453或La4728 凸包+枚举(或旋转卡壳)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 1393: Robert Hood 旋转卡壳 凸包
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1393 http://poj.org/problem?id=2187 Beauty Contest ...
- POJ 3608 Bridge Across Islands --凸包间距离,旋转卡壳
题意: 给你两个凸包,求其最短距离. 解法: POJ 我真的是弄不懂了,也不说一声点就是按顺时针给出的,不用调整点顺序. 还是说数据水了,没出乱给点或给逆时针点的数据呢..我直接默认顺时针给的点居然A ...
- 【BZOJ 1069】【SCOI 2007】最大土地面积 凸包+旋转卡壳
因为凸壳上对踵点的单调性所以旋转卡壳线性绕一圈就可以啦啦啦--- 先求凸包,然后旋转卡壳记录$sum1$和$sum2$,最后统计答案就可以了 #include<cmath> #includ ...
- 【POJ 2187】Beauty Contest(凸包直径、旋转卡壳)
给定点集的最远两点的距离. 先用graham求凸包.旋(xuán)转(zhuàn)卡(qiǎ)壳(ké)求凸包直径. ps:旋转卡壳算法的典型运用 http://blog.csdn.net/hanch ...
随机推荐
- APP全局异常捕获,并保存本地文件
public class CrashHandler implements Thread.UncaughtExceptionHandler { public static final String TA ...
- Java Random 含参与不含参构造函数的区别
##Random 通常用来作为随机数生成器,它有两个构造方法: Random random = new Random(); Random random2 = new Random(50); 1.不含参 ...
- JavaScript的BOM和DOM
JavaScript的BOM和DOM 1,window对象,所有浏览器都支持window对象,它表示浏览器窗口 BOM(browser Object Model)是指浏览器对象模型,它使JavaScr ...
- 《从零开始学Swift》学习笔记(Day 51)——扩展构造函数
原创文章,欢迎转载.转载请注明:关东升的博客 扩展类型的时候,也可以添加新的构造函数.值类型与引用类型扩展有所区别.值类型包括了除类以外的其他类型,主要是枚举类型和结构体类型. 值类型扩展构造函数 扩 ...
- explain the past and guide the future 好的代码的标准:解释过去,指引未来;
好的代码的标准:解释过去,指引未来: Design philosophies | Django documentation | Django https://docs.djangoproject.co ...
- 高德地图API使用
1.根据地址找经纬度/修改经纬度 marker.setPosition(result.geocodes[0].location); map.setCenter(marker.getPosition() ...
- MySQL事务隔离级别,锁(转)
add by zhj: 本文针对的是MySQL的InnoDB存储引擎,不适用于MySQL的其它存储引擎和其它数据库 原文:MySQL数据库事务隔离级别(Transaction Isolation Le ...
- Python获取主机名
import socket print socket.gethostname()
- ShutIt:一个基于 Python 的 shell 自动化框架
ShutIt是一个易于使用的基于shell的自动化框架.它对基于python的expect库(pexpect)进行了包装.你可以把它看作是“没有痛点的expect”.它可以通过pip进行安装. Hel ...
- Java基础—序列化与反序列化(转载)
转载自: Java序列化与反序列化 1.Java序列化与反序列化 Java序列化是指把Java对象转换为字节序列的过程:而Java反序列化是指把字节序列恢复为Java对象的过程. 2.为什么需要序列化 ...