题意:

求平面上的最远点对距离的平方。

分析:

对于这个数据量枚举肯定是要超时的。

首先这两个点一定是在凸包上的,所以可以枚举凸包上的点,因为凸包上的点要比原来的点会少很多,可最坏情况下的时间复杂度也是O(n2).

于是就有了旋转卡壳。

可以想象有两条平行直线紧紧地夹住这个凸包,那直线上的点就是对踵点对。对踵点对最多有四对,就是当凸包的两边和两直线重合的情况。

直线的角度不断变化,直线上的对踵点对也会发生变化,当直线旋转过180°后,那么凸包上所有的对踵点对也就全部遍历到了。

代码中还有很详细的注释。

里面是利用比较△(u, u+1, v) 和 △(u, u+1, v+1)的面积大小来寻找对踵点对的。因为是凸多边形,所以面积的比较转化成了两个叉积的比较,最后化简成了一个叉积PuPu+1×PvPv+1

直接从化简出来的结果来看,如果两个向量的叉乘大于0的话,说明v正在远离直线PuPu+1,如果小于0的话说明正在靠近直线,也很容易理解。

 //#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std; struct Point
{
int x, y;
Point(int x=, int y=):x(x), y(y){}
};
typedef Point Vector; Point operator + (Point a, Point b) { return Point(a.x+b.x, a.y+b.y); }
Point operator - (Point a, Point b) { return Point(a.x-b.x, a.y-b.y); }
int Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
int Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } bool operator < (const Point& a, const Point& b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
} bool operator == (const Point& a, const Point& b)
{
return a.x == b.x && a.y == b.x;
} 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); } 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();
//for(int i = 0; i < n; ++i) printf("%d %d\n", p[i].x, p[i].y);
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]
while(true)
{
// 当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]));
if(diff == ) ans = max(ans, Dist2(p[u], p[v+]));
break;
}
v = (v+)%n;
}
}
return ans;
} int main(void)
{
#ifdef LOCAL
freopen("4728in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
while(T--)
{
int n, x, y, w;
scanf("%d", &n);
vector<Point> p;
for(int i = ; 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+w, y+w));
p.push_back(Point(x, y+w));
}
printf("%d\n", diameter2(p));
} return ;
}

代码君

LA 4728 (旋转卡壳) Squares的更多相关文章

  1. LA 4728 旋转卡壳算法求凸包的最大直径

    #include<iostream> #include<cstdio> #include<cmath> #include<vector> #includ ...

  2. UVAL 4728 Squares(旋转卡壳)

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

  3. UVALive 4728 Squares(旋转卡壳)

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

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

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

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

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

  6. 1393: Robert Hood 旋转卡壳 凸包

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1393 http://poj.org/problem?id=2187 Beauty Contest ...

  7. POJ 3608 Bridge Across Islands --凸包间距离,旋转卡壳

    题意: 给你两个凸包,求其最短距离. 解法: POJ 我真的是弄不懂了,也不说一声点就是按顺时针给出的,不用调整点顺序. 还是说数据水了,没出乱给点或给逆时针点的数据呢..我直接默认顺时针给的点居然A ...

  8. 【BZOJ 1069】【SCOI 2007】最大土地面积 凸包+旋转卡壳

    因为凸壳上对踵点的单调性所以旋转卡壳线性绕一圈就可以啦啦啦--- 先求凸包,然后旋转卡壳记录$sum1$和$sum2$,最后统计答案就可以了 #include<cmath> #includ ...

  9. 【POJ 2187】Beauty Contest(凸包直径、旋转卡壳)

    给定点集的最远两点的距离. 先用graham求凸包.旋(xuán)转(zhuàn)卡(qiǎ)壳(ké)求凸包直径. ps:旋转卡壳算法的典型运用 http://blog.csdn.net/hanch ...

随机推荐

  1. 搭建Asp.Net MVC4

    启动vs2012,开始创建一个新的web应用程序.使用菜单:“文件”>“新建项目” 请在左侧选择 Visual C#,然后选择ASP.NET MVC 4 Web 应用程序.命名您的工程为&quo ...

  2. 3142:[HNOI2013]数列 - BZOJ

    题目描述 Description 小T最近在学着买股票,他得到内部消息:F公司的股票将会疯涨. 股票每天的价格已知是正整数,并且由于客观上的原因,最多只能为N.在疯涨的K天中小T观察到:除第一天外每天 ...

  3. 怎么让CentOS集群自动同步时间

    怎么让CentOS集群自动同步时间?首先机器要连外网,这样才能从互联网上同步时间,这是首先要了解的.好了,主要的方法如下: 在除了运行ntpd之外的机器上,执行: [html] # chkconfig ...

  4. 无废话网页重构系列——(6)HTML主干结构:站点(site)、页面(page)

    本文作者:大象本文地址:http://www.cnblogs.com/daxiang/p/4653546.html 在分析和切出设计稿,以及部署项目目录文件后,开始写HTML Demo. 首先,弄出H ...

  5. 【Memcache】下载和安装

    下载: Win7 64bit 系统 下载过过很多版本,都无法安装,最后到这里下载,成功安装: http://blog.couchbase.com/memcached-windows-64-bit-pr ...

  6. spring mvc 注解 annot失效

    如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行 这个问题是由于问答上有解决方案 引用 这个问题很经典了 在主容器中 ...

  7. 三层架构和MVC

    注:本文章内所有内容都来自互联网,本人主要是起了一个收集的作用 http://www.cnblogs.com/zhhh/archive/2011/06/10/2077519.html 又看到有人在问三 ...

  8. hdu 2102 A计划(BFS,基础)

    题目 //要仔细写的BFS,着重对#穿越的处理哦: //花了几个小时终于把这道简单的BFS给弄好了,我果然还需要增加熟练度,需要再仔细一些: //代码有点乱,但我不想改了,,,,, #include& ...

  9. UVA 11806 Cheerleaders (组合+容斥原理)

    自己写的代码: #include <iostream> #include <stdio.h> #include <string.h> /* 题意:相当于在一个m*n ...

  10. [Browsable(false)]

    1.c#方法上面的[Browsable(false)]是干吗用的? 答案:标明此对象不可被浏览,这样它就不会出现在设计器的属性窗口里了 看如下代码: /// <include file='Asp ...