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个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...
随机推荐
- RPM vs SRPM
RPM 全名是『 RedHat Package Manager 』简称则为 RPM 啦!顾名思义,当初这个软件管理的机制是由 Red Hat 这家公司发展出来的. RPM 是以一种数据库记录的方式来将 ...
- Android 自定义View修炼-Android实现圆形、圆角和椭圆自定义图片View(使用BitmapShader图形渲染方法)
一.概述 Android实现圆角矩形,圆形或者椭圆等图形,一般主要是个自定义View加上使用Xfermode实现的.实现圆角图片的方法其实不少,常见的就是利用Xfermode,Shader.本文直接继 ...
- MVC1笔记
/// ///直接返回 字符串的 Action方法,适用于 不需要返回大量 html代码的业务(类似于一般处理程序) /// public string Index() { return " ...
- javascript进击(八)JSON
JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Ob ...
- 使用POI操作Excel使用小总结
1. Workbook维护一个调色板,可以自定义设置56种颜色,下标从8到63. 用到颜色的地方,可以输入下标获取颜色,如CellStyle的setFillForegroundColor(); 2.C ...
- html 如何引入一个公共的头部和底部
2016-01-08 作者案:尽己之力,用心打造一个商城!当你有十二分力气时,发现用了十分力气依旧没什么进展,请不要放弃希望:或许你想得到的东西正在十二分力气的地方等你:假若你用尽了十二分力气,还是没 ...
- PeekMessage
PeekMessage是一个Windows API函数.该函数为一个消息检查线程消息队列,并将该消息(如果存在)放于指定的结构. 1 语法 BOOL PeekMessage( LPMSG IpMsg, ...
- 转载:在Visual Studio 2013中管理中国特色的社会主义Windows Azure
原文链接: http://www.pstips.net/get-azurechinacloud-settings.html 谷歌被豪迈地放弃了中国市场,微软仍旧在中国市场摸爬滚打,跪着挣钱.其中私人定 ...
- ios专题 -内存管理 研究
[原创]http://www.cnblogs.com/luoguoqiang1985 ARC [新的规则] 1. you cannot explicitly invoke dealloc, or im ...
- ES6的编码风格
编程风格 [转自http://es6.ruanyifeng.com/#docs/style] 块级作用域 字符串 解构赋值 对象 数组 函数 Map结构 Class 模块 ESLint的使用 本章探讨 ...