题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4199

没想透为啥旋转卡壳跟枚举跑时间差不多。n太小吧!

枚举法:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<queue>
  7. using namespace std;
  8. const int maxn = ;
  9. const int maxe = ;
  10. const int INF = 0x3f3f3f;
  11. const double eps = 1e-;
  12. const double PI = acos(-1.0);
  13.  
  14. struct Point{
  15. double x,y;
  16. Point(double x=, double y=) : x(x),y(y){ } //构造函数
  17. };
  18. typedef Point Vector;
  19.  
  20. Vector operator + (Vector A , Vector B){return Vector(A.x+B.x,A.y+B.y);}
  21. Vector operator - (Vector A , Vector B){return Vector(A.x-B.x,A.y-B.y);}
  22. Vector operator * (Vector A , double p){return Vector(A.x*p,A.y*p);}
  23. Vector operator / (Vector A , double p){return Vector(A.x/p,A.y/p);}
  24.  
  25. bool operator < (const Point& a,const Point& b){
  26. return a.x < b.x ||( a.x == b.x && a.y < b.y);
  27. }
  28.  
  29. int dcmp(double x){
  30. if(fabs(x) < eps) return ;
  31. else return x < ? - : ;
  32. }
  33. bool operator == (const Point& a, const Point& b){
  34. return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
  35. }
  36.  
  37. ///向量(x,y)的极角用atan2(y,x);
  38. double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
  39. double Length(Vector A) { return Dot(A,A); }
  40. double Angle(Vector A, Vector B) { return acos(Dot(A,B) / Length(A) / Length(B)); }
  41. double Cross(Vector A, Vector B) { return A.x*B.y - A.y * B.x; }
  42.  
  43. //凸包:
  44. /**Andrew算法思路:首先按照先x后y从小到大排序(这个地方没有采用极角逆序排序,所以要进行两次扫描),删除重复的点后得到的序列p1,p2.....,然后把p1和p2放到凸包中。从p3开始,当新的
  45. 点在凸包“前进”方向的左边时继续,否则依次删除最近加入凸包的点,直到新点在左边;**/
  46.  
  47. //Goal[]数组模拟栈的使用;
  48. int ConvexHull(Point* P,int n,Point* Goal){
  49. sort(P,P+n);
  50. int m = unique(P,P+n) - P; //对点进行去重;
  51. int cnt = ;
  52. for(int i=;i<m;i++){ //求下凸包;
  53. while(cnt> && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--;
  54. Goal[cnt++] = P[i];
  55. }
  56. int temp = cnt;
  57. for(int i=m-;i>=;i--){ //逆序求上凸包;
  58. while(cnt>temp && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--;
  59. Goal[cnt++] = P[i];
  60. }
  61. if(cnt > ) cnt--; //减一为了去掉首尾重复的;
  62. return cnt;
  63. }
  64. /*********************************分割线******************************/
  65.  
  66. Point P[maxn*],Goal[maxn*];
  67. int n;
  68.  
  69. int main()
  70. {
  71. //freopen("E:\\acm\\input.txt","r",stdin);
  72. int T;
  73. cin>>T;
  74. while(T--){
  75. cin>>n;
  76. int cnt = ;
  77. double x,y,w;
  78. for(int i=;i<=n;i++){
  79. scanf("%lf %lf %lf",&x,&y,&w);
  80. P[cnt++] = Point(x,y);
  81. P[cnt++] = Point(x+w,y);
  82. P[cnt++] = Point(x,y+w);
  83. P[cnt++] = Point(x+w,y+w);
  84. }
  85. cnt = ConvexHull(P,cnt,Goal);
  86. double Maxlen = ;
  87. for(int i=;i<cnt;i++)
  88. for(int j=i+;j<cnt;j++){
  89. Maxlen = max(Maxlen,Length(Goal[j]-Goal[i]));
  90. }
  91. printf("%.lf\n",Maxlen);
  92. }
  93. return ;
  94. }

旋转卡壳:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<queue>
  7. using namespace std;
  8. const int maxn = ;
  9. const int maxe = ;
  10. const int INF = 0x3f3f3f;
  11. const double eps = 1e-;
  12. const double PI = acos(-1.0);
  13.  
  14. struct Point{
  15. double x,y;
  16. Point(double x=, double y=) : x(x),y(y){ } //构造函数
  17. };
  18. typedef Point Vector;
  19.  
  20. Vector operator + (Vector A , Vector B){return Vector(A.x+B.x,A.y+B.y);}
  21. Vector operator - (Vector A , Vector B){return Vector(A.x-B.x,A.y-B.y);}
  22. Vector operator * (Vector A , double p){return Vector(A.x*p,A.y*p);}
  23. Vector operator / (Vector A , double p){return Vector(A.x/p,A.y/p);}
  24.  
  25. bool operator < (const Point& a,const Point& b){
  26. return a.x < b.x ||( a.x == b.x && a.y < b.y);
  27. }
  28.  
  29. int dcmp(double x){
  30. if(fabs(x) < eps) return ;
  31. else return x < ? - : ;
  32. }
  33. bool operator == (const Point& a, const Point& b){
  34. return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
  35. }
  36.  
  37. ///向量(x,y)的极角用atan2(y,x);
  38. double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
  39. double Length(Vector A) { return Dot(A,A); }
  40. double Angle(Vector A, Vector B) { return acos(Dot(A,B) / Length(A) / Length(B)); }
  41. double Cross(Vector A, Vector B) { return A.x*B.y - A.y * B.x; }
  42.  
  43. //凸包:
  44. /**Andrew算法思路:首先按照先x后y从小到大排序(这个地方没有采用极角逆序排序,所以要进行两次扫描),删除重复的点后得到的序列p1,p2.....,然后把p1和p2放到凸包中。从p3开始,当新的
  45. 点在凸包“前进”方向的左边时继续,否则依次删除最近加入凸包的点,直到新点在左边;**/
  46. // 如果不希望在凸包的边上有输入点,把两个 <= 改成 <
  47. //Goal[]数组模拟栈的使用;
  48. int ConvexHull(Point* P,int n,Point* Goal){
  49. sort(P,P+n);
  50. int m = unique(P,P+n) - P; //对点进行去重;
  51. int cnt = ;
  52. for(int i=;i<m;i++){ //求下凸包;
  53. while(cnt> && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--;
  54. Goal[cnt++] = P[i];
  55. }
  56. int temp = cnt;
  57. for(int i=m-;i>=;i--){ //逆序求上凸包;
  58. while(cnt>temp && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--;
  59. Goal[cnt++] = P[i];
  60. }
  61. if(cnt > ) cnt--; //减一为了去掉首尾重复的;
  62. return cnt;
  63. }
  64. //旋转卡壳可以用于求凸包的直径、宽度,两个不相交凸包间的最大距离和最小距离
  65. //计算凸包直径,输入凸包Goal,顶点个数为n,按逆时针排列,输出直径的平方
  66. double RotatingCalipers(Point* Goal,int n){
  67. double ret = ;
  68. Goal[n]=Goal[]; //补上使凸包成环;
  69. int pv = ;
  70. for(int i=;i<n;i++){ //枚举边Goal[i]Goal[i+1],与最远顶点Goal[pv];利用叉积求面积的方法求最大直径;;
  71. while(fabs(Cross(Goal[i+]-Goal[pv+],Goal[i]-Goal[pv+]))>fabs(Cross(Goal[i+]-Goal[pv],Goal[i]-Goal[pv])))
  72. pv = (pv+)%n;
  73. ret=max(ret,max(Length(Goal[i]-Goal[pv]),Length(Goal[i+]-Goal[pv+]))); //这个地方不太好理解,就是要考虑当pv与pv+1所在直线平行于i与i+1的情况;
  74. }
  75. return ret;
  76. }
  77. /*********************************分割线******************************/
  78.  
  79. Point P[maxn*],Goal[maxn*];
  80. int n;
  81.  
  82. int main()
  83. {
  84. //freopen("E:\\acm\\input.txt","r",stdin);
  85. int T;
  86. cin>>T;
  87. while(T--){
  88. cin>>n;
  89. int cnt = ;
  90. double x,y,w;
  91. for(int i=;i<=n;i++){
  92. scanf("%lf %lf %lf",&x,&y,&w);
  93. P[cnt++] = Point(x,y);
  94. P[cnt++] = Point(x+w,y);
  95. P[cnt++] = Point(x,y+w);
  96. P[cnt++] = Point(x+w,y+w);
  97. }
  98. cnt = ConvexHull(P,cnt,Goal);
  99. double Maxlen = RotatingCalipers(Goal,cnt);
  100. printf("%.lf\n",Maxlen);
  101. }
  102. return ;
  103. }

UVa1453或La4728 凸包+枚举(或旋转卡壳)的更多相关文章

  1. hdu 2187(凸包直径 1.枚举 2.旋转卡壳)

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33115   Accepted: 10278 ...

  2. [Bzoj1069][Scoi2007]最大土地面积(凸包)(旋转卡壳)

    1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 3629  Solved: 1432[Submit][Sta ...

  3. 【POJ 2187】Beauty Contest(凸包直径、旋转卡壳)

    给定点集的最远两点的距离. 先用graham求凸包.旋(xuán)转(zhuàn)卡(qiǎ)壳(ké)求凸包直径. ps:旋转卡壳算法的典型运用 http://blog.csdn.net/hanch ...

  4. [模板] 计算几何2: 自适应Simpson/凸包/半平面交/旋转卡壳/闵可夫斯基和

    一些基本的定义在这里: [模板] 计算几何1(基础): 点/向量/线/圆/多边形/其他运算 自适应Simpson Simpson's Rule: \[ \int ^b_a f(x)dx\approx ...

  5. BZOJ 1185 [HNOI2007]最小矩形覆盖:凸包 + 旋转卡壳

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1185 题意: 给出二维平面上的n个点,问你将所有点覆盖的最小矩形面积. 题解: 先找出凸 ...

  6. 【BZOJ 1069】【SCOI 2007】最大土地面积 凸包+旋转卡壳

    因为凸壳上对踵点的单调性所以旋转卡壳线性绕一圈就可以啦啦啦--- 先求凸包,然后旋转卡壳记录$sum1$和$sum2$,最后统计答案就可以了 #include<cmath> #includ ...

  7. UVA 4728 Squares(凸包+旋转卡壳)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...

  8. 【旋转卡壳+凸包】BZOJ1185:[HNOI2007]最小矩形覆盖

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

  9. HDU 5251 矩形面积(二维凸包旋转卡壳最小矩形覆盖问题) --2015年百度之星程序设计大赛 - 初赛(1)

    题目链接   题意:给出n个矩形,求能覆盖所有矩形的最小的矩形的面积. 题解:对所有点求凸包,然后旋转卡壳,对没一条边求该边的最左最右和最上的三个点. 利用叉积面积求高,利用点积的性质求最左右点和长度 ...

随机推荐

  1. postgresql sql修改表,表字段

    1.更改表名 alter table 表名 rename to 新表名 2.更改字段名 alter table 表名 rename 字段名 to 新字段名 3.增加列 ALTER TABLE ud_w ...

  2. Const和ReadOnly

    总结一下const和readonly有这么几条区别: const和readonly的值一旦初始化则都不再可以改写: const只能在声明时初始化:readonly既可以在声明时初始化也可以在构造器中初 ...

  3. 实现HTTP跳转到HTTPS

    1 首先在您的网站下新建一个站点,名称随意,在属性中分配TCP端口为80,SSL不分配 然后在属性>主目录下配置 将此资源的内容来自: 改为 重定向到URL 然后重定向到中  输入:  HTTP ...

  4. iOS自动布局之autoresizingi

    对于iOS的app开发者来说,不会像Android开发者一样为很多的屏幕尺寸来做界面适配,因此硬编码的坐标也能工作良好,但是从设计模式上来说这不是好的做法.而且也还有一些问题,如iPhone5的适配, ...

  5. SGU 200.Cracking RSA(高斯消元)

    时间限制:0.25s 空间限制:4M 题意: 给出了m(<100)个数,这m个数的质因子都是前t(<100)个质数构成的. 问有多少个这m个数的子集,使得他们的乘积是完全平方数. Solu ...

  6. 那些年,我们一起被坑的H5音频

    原文地址:http://weibo.com/p/23041874d6cedd0102vkbr   不要被这么文艺的标题吓到,这里不会跟你讲述中学时期泡妞史,也不会有其它什么现实不该有而小说噼里啪啦不能 ...

  7. TestNG目录

    1 - 简介  2 - 注解  3 - testng.xml  4 - 执行 TestNG  5 - 测试方法, 测试类 和 测试组    5.1 - 测试方法    5.2 - 测试组    5.3 ...

  8. centos7/RHEL7安装LibreOffice

    1.下载 wget http://download.documentfoundation.org/libreoffice/testing/4.4.0/rpm/x86_64/LibreOfficeDev ...

  9. 批处理文件安装与卸载Windows服务

    //安装Windows服务 将RECPost.exe和RECPostService替换成自己的项目名称和服务名称,并将文件保存成bat格式.其中%cd%是获取相对路径 @echo off set fi ...

  10. Smart Client Software Factory安装

    首先要安装 Visual Studio 2010 SDK 不然无法安装 Smart Client Software Factory 2010 然后按顺序安装 GAX 2010 http://visua ...