旋转卡壳算法;

直接在这个上面粘的模板

主要用途:用于求凸包的直径、宽度,两个不相交凸包间的最大距离和最小距离···

这题就是求凸包的直径

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#define eps 1e-9
using namespace std;
const double pi = acos(-); int dcmp(double x)
{
return fabs(x) < eps ? : (x > ? : -);
} struct Point
{
double x;
double y; Point(double x = , double y = ):x(x), y(y) {} bool operator < (const Point& e) const
{
return dcmp(x - e.x) < || (dcmp(x - e.x) == && dcmp(y - e.y) < );
} bool operator == (const Point& e) const
{
return dcmp(x - e.x) == && dcmp(y - e.y) == ;
}
}; typedef Point Vector; Vector operator + (Point A, Point B)
{
return Vector(A.x + B.x, A.y + B.y);
} Vector operator - (Point A, Point B)
{
return Vector(A.x - B.x, A.y - B.y);
} Vector operator * (Point A, double p)
{
return Vector(A.x * p, A.y * p);
} Vector operator / (Point A, double p)
{
return Vector(A.x / p, A.y / p);
}
double dot(Point a,Point b)
{
return a.x*b.x+a.y*b.y;
}
double cross(Point a,Point b)
{
return a.x*b.y-a.y*b.x;
}
Point rotate(Point a,double ang)
{
return Point(a.x*cos(ang)-a.y*sin(ang),a.x*sin(ang)+a.y*cos(ang));
}
int convexhull(Point *p,int n,Point *ch)
{
sort(p,p+n);
int m=;
for(int i=; i<n; i++)
{
while(m>&&cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-; i>=; i--)
{
while(m>k&&cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)m--;
ch[m++]=p[i];
}
if(n>)m--;
return m;
}
bool onsegment(Point p,Point a,Point b)
{
return dcmp(cross(a-p,b-p))==&&dcmp(dot(a-p,b-p))<;
} bool SegmentProperIntersection( Point a1, Point a2, Point b1, Point b2 ) //线段相交,交点不在端点
{
double c1 = cross( a2 - a1, b1 - a1 ), c2 = cross( a2 - a1, b2 - a1 ),
c3 = cross( b2 - b1, a1 - b1 ), c4 = cross( b2 - b1, a2 - b1 );
return dcmp(c1)*dcmp(c2) < && dcmp(c3) * dcmp(c4) < ;
} int ispointinpolygon(Point p,int n,Point *poly)
{
int wn=;
for(int i=; i<n; i++)
{
if(onsegment(p,poly[i],poly[(i+)%n]))return -;
int k=dcmp(cross(poly[(i+)%n]-poly[i],p-poly[i]));
int d1=dcmp(poly[i].y-p.y);
int d2=dcmp(poly[(i+)%n].y-p.y);
if(k>&&d1<=&&d2>)wn++;
if(k<&&d2<=&&d1>)wn--;
}
if(wn!=)return ;
return ;
} bool Check(int n,Point *ch,int m,Point *th)
{
for(int i=; i<n; i++)
{
if(ispointinpolygon(ch[i],m,th)!=)return ;
}
for(int i=; i<m; i++)
if(ispointinpolygon(th[i],n,ch)!=)return ;
ch[n]=ch[];
th[m]=th[];
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(SegmentProperIntersection(ch[i],ch[i+],th[j],th[j+]))return ;
return ;
}
double rotating_calipers(Point *ch,int n)
{
int q=;
double ans=;
ch[n]=ch[];
for ( int i = ; i < n; ++i )
{
while ( cross( ch[i + ] - ch[i], ch[q + ] - ch[i] ) > cross( ch[i + ] - ch[i], ch[q] - ch[i] ) )
q = ( q + ) % n;
ans = max( ans, max( dot( ch[i]- ch[q],ch[i]-ch[q] ),dot( ch[i + ]-ch[q + ],ch[i + ]-ch[q + ] ) ));
}
return ans;
}
Point p[],ch[];
int main()
{
int n,m;
double x,y,w;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int cnt=;
for(int i=; i<n; i++)
{
scanf("%lf%lf%lf",&x,&y,&w);
p[cnt].x=x,p[cnt++].y=y;
p[cnt].x=x+w,p[cnt++].y=y;
p[cnt].x=x,p[cnt++].y=y+w;
p[cnt].x=x+w,p[cnt++].y=y+w;
}
int n1=convexhull(p,cnt,ch);
printf("%.0lf\n",rotating_calipers(ch,n1));
}
return ;
}

uva 1453 - Squares的更多相关文章

  1. UVa 1453 - Squares 旋转卡壳求凸包直径

    旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...

  2. UVa 201 Squares

    题意: 给出这样一个图,求一共有多少个大小不同或位置不同的正方形. 分析: 这种题一看就有思路,最开始的想法就是枚举正方形的位置,需要二重循环,枚举边长一重循环,判断是否为正方形又需要一重循环,复杂度 ...

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

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

  4. 【每日一题】Squares UVA - 201 暴力+输出坑 + 读文件模板

    题意 给你n*n的图,让你数正方形 题解:暴力for每个点,对于每个点从它出发顺时针走一个正方形.走完就ans[i]++; 坑:多输了一行******,然后在那里手摸样例,无限debug orz #d ...

  5. 【UVA】201 Squares(模拟)

    题目 题目     分析 记录一下再预处理一下.     代码 #include <bits/stdc++.h> int main() { int t=0,s,n; while(scanf ...

  6. Squares UVA - 201

    A children's board game consists of a square array of dots that contains lines connecting some of th ...

  7. UVa 1643 Angle and Squares

    题意: 如图,有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 分析: 直观上来看,当这n个正方形的对角线在一条直线上时,封闭区域的面积最大.( ...

  8. UVA 12113 Overlapping Squares

    题意: 总共有6个2*2的正方形,判断是否能够成所给的形状. 思路: 一个正方形总共有9种摆放方式,对于整个地图来说摆放方式总共有2的9次方种摆放方式.然后将地图用9*5的数组表示,正方形的位置用其8 ...

  9. UVa 1643 Angle and Squares (计算几何)

    题意:有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...

随机推荐

  1. 关于Intent的七大属性

    原谅我愚昧,Intent七大属性这个概念我也是昨天才接触到,看了一下,都是一些常用的东西,就是没有总结过,那么今天就来简单总结一下. Intent七大属性是指Intent的ComponentName. ...

  2. HTML解析引擎:Jumony 开源项目

    Jumony Core首先提供了一个近乎完美的HTML解析引擎,其解析结果无限逼近浏览器的解析结果.不论是无结束标签的元素,可选结束标签的元素,或是标记属性,或是CSS选择器和样式,一切合法的,不合法 ...

  3. cognos 10.2.2 导入samples数据源报错解决

    操作系统:windows 2008R2 X64 数据库:oracle 10.2.0.1 X32 sid:cognosdb86 安装完samples后,执行IBM安装目录webcontent\sampl ...

  4. RS485接线 - 为什么要给2线制RS485接3根线?

    http://www.chipkin.com/rs485-cables-why-you-need-3-wires-for-2-two-wire-rs485/ RS485needs 3 conducto ...

  5. C#操作redis代码汇总

    马上要用redis来改造现有的o2o项目了,在linux下部署了个redis,顺便研究了下代码操作,分享下代码 using System; using System.Collections.Gener ...

  6. Unity中使用RequireComponent,没有添加上组件

    using UnityEngine; using System.Collections; [RequireComponent(typeof(MeshFilter), typeof(MeshRender ...

  7. 九度0J 1374 所有员工年龄排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1374 题目描述: 公司现在要对所有员工的年龄进行排序,因为公司员工的人数非常多,所以要求排序算法的效率要非常高,你 ...

  8. SQL优化(2)

    建表时候数据库引擎的选择也可以达到优化的效果 InnoDB: 基于磁盘的资源是InnoDB表空间数据文件和它的日志文件,InnoDB 表的大小只受限于操作系统文件的大小,一般为 2GB MyISAM: ...

  9. Nginx 第三方模块-漫谈缘起

    http://www.cnblogs.com/yjf512/archive/2012/03/30/2424726.html http://chenxiaoyu.org/2011/10/30/nginx ...

  10. 检测 IE 版本 in Javascript

    点击打开链接http://stackoverflow.com/questions/10964966/detect-ie-version-in-javascript <!doctype html& ...