题意:求所有正方形中两点距离最大值的平方值。

思路:旋转卡壳法。

分别用数组和vector存凸包时,旋转卡壳代码有所不同。

 #include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<memory.h>
#include<cstdlib>
#include<vector>
#define clc(a,b) memset(a,b,sizeof(a))
#define LL long long int
#define up(i,x,y) for(i=x;i<=y;i++)
#define w(a) while(a)
using namespace std;
const double inf=0x3f3f3f3f;
const int N = ;
const double eps = *1e-;
const double PI = acos(-1.0);
using namespace std; struct Point
{
int x, y;
Point(int x=, int y=):x(x),y(y) { }
}; typedef Point Vector; Vector operator - (const Point& A, const Point& B)
{
return Vector(A.x-B.x, A.y-B.y);
} int Cross(const Vector& A, const Vector& B)
{
return A.x*B.y - A.y*B.x;
} int Dot(const Vector& A, const Vector& B)
{
return A.x*B.x + A.y*B.y;
} int Dist2(const Point& A, const Point& B)
{
return (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y);
} bool operator < (const Point& p1, const Point& p2)
{
return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);
} bool operator == (const Point& p1, const Point& p2)
{
return p1.x == p2.x && p1.y == p2.y;
} // 点集凸包
// 如果不希望在凸包的边上有输入点,把两个 <= 改成 <
// 注意:输入点集会被修改
vector<Point> ConvexHull(vector<Point>& p)
{
// 预处理,删除重复点
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end()); int n = p.size();
int m = ;
vector<Point> ch(n+);
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--;
ch.resize(m);
return ch;
} // 返回点集直径的平方
int diameter2(vector<Point>& points)
{
vector<Point> p = ConvexHull(points);
int n = p.size();
if(n == ) return ;
if(n == ) return Dist2(p[], p[]);
p.push_back(p[]); // 免得取模
int ans = ;
for(int u = , v = ; u < n; u++)
{
// 一条直线贴住边p[u]-p[u+1]
for(;;)
{
// 当Area(p[u], p[u+1], p[v+1]) <= Area(p[u], p[u+1], p[v])时停止旋转
// 即Cross(p[u+1]-p[u], p[v+1]-p[u]) - Cross(p[u+1]-p[u], p[v]-p[u]) <= 0
// 根据Cross(A,B) - Cross(A,C) = Cross(A,B-C)
// 化简得Cross(p[u+1]-p[u], p[v+1]-p[v]) <= 0
int diff = Cross(p[u+]-p[u], p[v+]-p[v]);
if(diff <= )
{
ans = max(ans, Dist2(p[u], p[v])); // u和v是对踵点
if(diff == ) ans = max(ans, Dist2(p[u], p[v+])); // diff == 0时u和v+1也是对踵点
break;
}
v = (v + ) % n;
}
}
return ans;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
vector<Point> points;
for(int i = ; i < n; i++)
{
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
points.push_back(Point(x, y));
points.push_back(Point(x+w, y));
points.push_back(Point(x, y+w));
points.push_back(Point(x+w, y+w));
}
printf("%d\n", diameter2(points));
}
return ;
}

uvalive 4728 Squares的更多相关文章

  1. UVALive 4728 Squares(旋转卡壳)

    Squares The famous Korean IT company  plans to make a digital map of the Earth with help of wireless ...

  2. UVALive 4728 Squares (平面最远点对)

    题意:n个平行于坐标轴的正方形,求出最远点对的平方 题解:首先求出凸包,可以证明最远点对一定是凸包上的点对,接着可以证明最远点对(每个点的对踵点)一定只有3*n/2对 接着使用旋转卡壳找到最远点对,但 ...

  3. UVAL 4728 Squares(旋转卡壳)

    Squares [题目链接]Squares [题目类型]旋转卡壳 &题解: 听着算法名字,感觉挺难,仔细一看之后,发现其实很简单,就是依靠所构成三角行面积来快速的找对踵点,就可以省去很多的复杂 ...

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

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

  5. UVALive 4025 Color Squares(BFS)

    题目链接:UVALive 4025 Color Squares 按题意要求放带有颜色的块,求达到w分的最少步数. //yy:哇,看别人存下整个棋盘的状态来做,我什么都不想说了,不知道下午自己写了些什么 ...

  6. UVaLive 6602 Counting Lattice Squares (找规律)

    题意:给定一个n*m的矩阵,问你里面有几面积为奇数的正方形. 析:首先能知道的是,大的矩阵是包括小的矩阵的,而且面积为奇数,我们只要考虑恰好在边界上的正方形即可,画几个看看就知道了,如果是3*3的有3 ...

  7. LA 4728 (旋转卡壳) Squares

    题意: 求平面上的最远点对距离的平方. 分析: 对于这个数据量枚举肯定是要超时的. 首先这两个点一定是在凸包上的,所以可以枚举凸包上的点,因为凸包上的点要比原来的点会少很多,可最坏情况下的时间复杂度也 ...

  8. UVALive 6602 Counting Lattice Squares

    给定一个n*m的网格,求面积为奇数的正方形有多少个. 首先是n*m个面积为1的,然后剩下的要么是边长为奇数,要么被这样一个奇数边长所包围. 原因如下: 对于一个边长不平行于坐标抽的正方形,其边长一定是 ...

  9. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

随机推荐

  1. maven插件mybatis-generator生成代码配置

    鸣谢:http://my.oschina.net/u/1763011/blog/324106?fromerr=nJakGh4P (也可参看此博客进行配置) http://www.cnblogs.com ...

  2. 写给 iOS 开发者的 Hopper + lldb 简介

    最近,关于 @Steipete 在Radar发布的帖子,笔者看到很多人在问「你是怎么理解那个伪代码的」.笔者想写博客已经有一段时间了,现在正好就此发表第一篇博文.笔者在一个叫 Hopper 的工具上花 ...

  3. codeforces #305 div1 done

    总算搞定了这一场比赛的题目,感觉收获蛮大 其中A,B,C都能通过自己的思考解决掉 D题思路好神,E题仔细想想也能想出来 以后坚持每两天或者一天做一场CF的div1的全套题目 除非有实在无法做出来的题目 ...

  4. 命令行添加用户的“作为服务登录”权利(添加Windows用户的时候,门道不是一般的多)good

    1.打开控制台(“开始”|“运行”中输入:MMC) 2.“文件”菜单|“添加删除管理单元”|“添加...”|选“安全模板”|“关闭”. 3.在“C:\Windows\Security\template ...

  5. Tomcat 部署Undeployment Failure

    Tomcat 部署Undeployment Failure - yongjava的日志 - 网易博客 http://blog.163.com/qiangyongbin2000@126/blog/sta ...

  6. 安装Ubuntu双系统系列——64位Ubuntu安装H3C的INode客户端

    学校使用的是Inode客户端认证上网的.如果是使用Ubuntu 32位版本,可以完美地安装并能够连接到网站.但是如果安装的是Ubuntu desktop 12.10 amd64版本,则发现之前的&qu ...

  7. 关于xml的一些知识,DTD,XSD

    DTD 文档类型定义(Document Type Definition)是一套关于标记符的语法规则.它是标准通用标记语言和 可扩展标记语言1.0版规格的一部分,是文档的验证机制.文档类型定义是一种保证 ...

  8. CodeWars题目筛选

    http://www.codewars.com/kata/search/csharp?q=&r%5B%5D=-8&xids=completed&beta=false 语言选择C ...

  9. 函数flst_init

    /** The null file address */UNIV_INTERN fil_addr_t fil_addr_null = {FIL_NULL, 0}; /***************** ...

  10. Maven相关内容学习笔记一:基本配置和使用

    首先必须推荐的这本书<Maven实战> 许晓斌,机械工业出版社 Maven简介 其实使用Maven也有很久时间了,大部分都是别人建好了工程我使用一下,实际上并没有非常详细的使用经验,这次到 ...