uva 1453 - Squares
旋转卡壳算法;
直接在这个上面粘的模板
主要用途:用于求凸包的直径、宽度,两个不相交凸包间的最大距离和最小距离···
这题就是求凸包的直径
#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的更多相关文章
- UVa 1453 - Squares 旋转卡壳求凸包直径
旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...
- UVa 201 Squares
题意: 给出这样一个图,求一共有多少个大小不同或位置不同的正方形. 分析: 这种题一看就有思路,最开始的想法就是枚举正方形的位置,需要二重循环,枚举边长一重循环,判断是否为正方形又需要一重循环,复杂度 ...
- UVA 4728 Squares(凸包+旋转卡壳)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...
- 【每日一题】Squares UVA - 201 暴力+输出坑 + 读文件模板
题意 给你n*n的图,让你数正方形 题解:暴力for每个点,对于每个点从它出发顺时针走一个正方形.走完就ans[i]++; 坑:多输了一行******,然后在那里手摸样例,无限debug orz #d ...
- 【UVA】201 Squares(模拟)
题目 题目 分析 记录一下再预处理一下. 代码 #include <bits/stdc++.h> int main() { int t=0,s,n; while(scanf ...
- Squares UVA - 201
A children's board game consists of a square array of dots that contains lines connecting some of th ...
- UVa 1643 Angle and Squares
题意: 如图,有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 分析: 直观上来看,当这n个正方形的对角线在一条直线上时,封闭区域的面积最大.( ...
- UVA 12113 Overlapping Squares
题意: 总共有6个2*2的正方形,判断是否能够成所给的形状. 思路: 一个正方形总共有9种摆放方式,对于整个地图来说摆放方式总共有2的9次方种摆放方式.然后将地图用9*5的数组表示,正方形的位置用其8 ...
- UVa 1643 Angle and Squares (计算几何)
题意:有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...
随机推荐
- easy_painting
最近感觉结构,比例抓的容易多了.
- 转:ORACLEERP开发基础之EBS开发基础
转自:http://blog.itpub.net/8781091/viewspace-1012244/ [内容导航] 第1页:开发工具安装 第2页:增加数据块 第3页:注册表单FORM 第4页:注册请 ...
- pcap支持Python2.7.8解决办法
pcap库只支持到python2.5. pip install pcap在python2.7.8找不到. 只需要将网盘的2个文件放到python安装目录下lib/site-package文件夹即可 链 ...
- PAT L1-009. N个数求和
本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数“分子/分母”的形式给出的,你输出的和也必须是有理数的形式. 输入格式: 输入第一行给出一个正整数N(<=100).随后一行按格 ...
- Magento网站如何添加一个可配置产品
有的产品,比如服装,同一件衣服有S.M.L.XL.XXL等尺码供客户选择,或者有多种颜色可以供客户选择,Magento中管这种有选项供客户选择的产品叫做可配置产品 (Configurable Prod ...
- box-shadow 与 filter:drop-shadow 详解及奇技淫巧
box-shadow 在前端的 CSS 编写工作想必十分常见.但是 box-shadow 除去它的常规用法,其实还存在许多不为人知的奇技淫巧. 喜欢 markdown 版本的可以戳这里. box-sh ...
- 那天有个小孩跟我说LINQ(五)转载
2 LINQ TO SQL(代码下载) 我们以一个简单的销售的业务数据库为例子 表结构很简单:Users(购买者(用户)表),Products(产品信息表),Sales(销 ...
- excel导入 导出 兼容各个版本服务器不装EXCEL也可以
给出 demo源码: http://pan.baidu.com/s/1hqGMudY 提取码:pw4n首先要引用 NPOI.dll (可在网上下载!) //导入 public void OnSubmi ...
- 【BZOJ3529】【莫比乌斯反演 + 树状数组】[Sdoi2014]数表
Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为 能同时整除i和j的所有自然数之和.给定a,计算数表中不大于 ...
- 原型模式(Prototype Pattern)
原型模型:用于创建重复对象,同时保证性能. 这种模式实现一个原型接口,用于创建对象的克隆,当直接创建对象的代价比较大,则可以采用这种模式.例如:一个对象需要高代价的数据库操作之后被创建,这时可以缓存该 ...