#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std; struct Point {
int x, y;
Point(int x=0, int y=0):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;
} int max(int a,int b)
{
return a>b?a:b;
} vector<Point> ConvexHull(vector<Point>& p) //求凸包
{
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end());
int i,n = p.size();
int m = 0;
vector<Point> ch(n+1);
for(i = 0; i < n; i++) {
while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
ch[m++] = p[i];
}
int k = m;
for(i = n-2; i >= 0; i--) {
while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
ch[m++] = p[i];
}
if(n > 1) m--;
ch.resize(m);
return ch;
} int diameter2(vector<Point>& points)//求凸包的最大直径(旋转卡壳算法)
{
vector<Point> p = ConvexHull(points);
int n = p.size();
if(n == 1) return 0;
if(n == 2) return Dist2(p[0], p[1]);
p.push_back(p[0]);
int ans = 0;
int i=0,j=1;
for(;i<n;i++)
{
while(Cross(p[i+1]-p[i], p[j+1]-p[i]) > Cross(p[i+1]-p[i], p[j]-p[i]))
j=(j+1)%n;
ans=max(ans,max(Dist2(p[i],p[j]),Dist2(p[i+1],p[j+1])));
}
return ans;
} int main()
{
int T,i,n,x,y,w;
vector<Point> P;
scanf("%d",&T);
while(T--)
{
P.clear();
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d%d", &x, &y, &w);
P.push_back(Point(x, y));
P.push_back(Point(x+w, y));
P.push_back(Point(x, y+w));
P.push_back(Point(x+w, y+w));
}
printf("%d\n", diameter2(P));
}
return 0;
}

LA 4728 旋转卡壳算法求凸包的最大直径的更多相关文章

  1. POJ2187 Beauty Contest (旋转卡壳算法 求直径)

    POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...

  2. LA 4728 (旋转卡壳) Squares

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

  3. Gym - 101635K:Blowing Candles (简单旋转卡壳,求凸包宽度)

    题意:给定N个点,用矩形将所有点覆盖,要求矩形宽度最小. 思路:裸体,旋转卡壳去rotate即可. 最远距离是点到点:宽度是点到边. #include<bits/stdc++.h> #de ...

  4. poj 2187 凸包加旋转卡壳算法

    题目链接:http://poj.org/problem?id=2187 旋转卡壳算法:http://www.cppblog.com/staryjy/archive/2009/11/19/101412. ...

  5. POJ 2079 Triangle(凸包+旋转卡壳,求最大三角形面积)

    Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 7625   Accepted: 2234 Descript ...

  6. POJ 3608 Bridge Across Islands(旋转卡壳,两凸包最短距离)

    Bridge Across Islands Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7202   Accepted:  ...

  7. BZOJ 1185: [HNOI2007]最小矩形覆盖-旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标-备忘板子

    来源:旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标 BZOJ又崩了,直接贴一下人家的代码. 代码: #include"stdio.h" #include"str ...

  8. poj 3608(旋转卡壳求解两凸包之间的最短距离)

    Bridge Across Islands Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9768   Accepted: ...

  9. POJ - 2187:Beauty Contest (最简单的旋转卡壳,求最远距离)

    Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the ti ...

随机推荐

  1. COGS 696. [IOI1996][USACO 2.3] 最长前缀

    ★   输入文件:prefix.in   输出文件:prefix.out   简单对比时间限制:1 s   内存限制:128 MB 描述 USACO 2.3.1 IOI96 在生物学中,一些生物的结构 ...

  2. php随机生成国内ip地址

    获得一个国家所有ip段,随机生成国内ip地址的缩水实现.注意:  $ip_long数组中后5个值在64位系统中可能是错误的(下面代码中  $ip_long 数组的后五个值在32位系统中为负数,64位系 ...

  3. (四)maven之查找jar包坐标,选择jar包版本

    ①    先访问http://www.mvnrepository.com/  ,这个地址是maven的公共库. ②   以spring core的jar包为例.在页面的最上方的中间,输入spring ...

  4. 怎么在webstorm中设置代码模板

    大家都知道webstorm对程序员来说是一个很好用的IDE.我们输入几个关键字,webstorm就会给出提示,大大提高了我们的开发效率,可有时候webstorm的默认设置不能满足我们的个性化代码模板的 ...

  5. QT +自定义控件-spin+slider

    动手实现自定义控件: 1.首先在ui界面中添加一个(Widget)容器类.如图中的1所示 2.在项目中添加一个SmallWidget类,如下: 3.接着在程序编辑界面进行程序编辑如下: #includ ...

  6. KeyValuePair的使用

    Dictionary<string, string> dc = new Dictionary<string, string>(); 前台页面: <div id=" ...

  7. PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)

    http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...

  8. Greenplum介绍-table

    GP中的table和其它关系型数据表是一样的,除了数据被分布在不同的segment以外. 建表时需定义以下几个方面:1. 指定列和数据类型2. 约束3. 分布策略4. 数据存储方式5. 大表分区策略 ...

  9. (12)zabbix agent 类型所有key

    zabbix服务器端通过与zabbix agent通信来获取客户端服务器的数据,agent分为两个版本,其中一个是主动一个是被动,在配置主机我们可以看到一个是agent,另一个是agent(activ ...

  10. Ubuntu中安装配置 JDK与apache

    一,前期准备: 1.下载apach网址:https://tomcat.apache.org/download-90.cgi 3.下载:jdk网址:http://www.oracle.com/techn ...