Squares

【题目链接】Squares

【题目类型】旋转卡壳

&题解:

听着算法名字,感觉挺难,仔细一看之后,发现其实很简单,就是依靠所构成三角行面积来快速的找对踵点,就可以省去很多的复杂度了.旋转的复杂度是O(n),之后还有计算每条边对应的对踵点复杂度平均大约O(n/2)在实际中也许可以更快

&代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
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 Vector& A,const Vector& B) {return Vector(A.x-B.x , A.y-B.y);}
bool operator == (const Point& a,const Point& b) {return a.x==b.x&&a.y==b.y;}
bool operator < (const Point& a,const Point& b) {return a.x<b.x||(a.x==b.x&&a.y<b.y);}
int Cross(Vector A,Vector B) {return A.x*B.y - 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(), m=0;
vector<Point> ch(n+1);
for(int i=0; i<n; i++) {
while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-1])<=0) m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2; i>=0; i--) {
while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-1])<=0) m--;
ch[m++]=p[i];
}
if(m>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;
for(int u=0, v=1; u<n; u++) {
for(;;) {
int diff = Cross(p[u+1]-p[u] , p[v+1]-p[v]);
if( diff<=0 ) {
ans=max(ans , Dist2(p[u],p[v]));
//2个三角形面积相等
if(diff==0) ans=max(ans, Dist2(p[u], p[v+1]));
break;
}
v=(v+1)%n;
}
}
return ans;
} int main() {
freopen("E:1.in","r",stdin);
int T;
scanf("%d",&T);
while(T--) {
int n;
scanf("%d" ,&n);
vector<Point> po;
for(int i=0; i<n; i++) {
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
po.push_back(Point(x,y));
po.push_back(Point(x+w,y));
po.push_back(Point(x,y+w));
po.push_back(Point(x+w,y+w));
}
printf("%d\n", diameter2(po));
}
return 0;
}

UVAL 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. UVa 1453 - Squares 旋转卡壳求凸包直径

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

  3. LA 4728 (旋转卡壳) Squares

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. MySQL8.0安装连接Navicat的坑

    刚在官网装好MySQL8.0后,我的cmd识别不了启动数据库的指令 需要cd到MySQL的bin目录配置mysql mysqld --install mysqld --remove mysql -u ...

  2. [Day1]常用Dos命令,Java相关描述及基础

    1.常用的DOS命令 (1)返回上一级目录:cd.. (2)返回盘符根目录:cd\ (3)切换当前盘符:   盘符: (4)进入文件夹:       cd 文件路径 (5)展示当前目录下的所有内容:D ...

  3. IndentationError: expected an indented block 在继承中出现的问题:未完

    1. class Foo(object): def __init__(self,name,price,period): self.name=name self.price=price self.per ...

  4. Instruments之Activity Monitor使用入门

    Activity Monitor,官方解释为:(活动监视器)即实时显示CPU.内存和网络的使用情况,记录由虚拟内存大小测量的系统负载.用一句大白话来说,Activity Monitor类似Window ...

  5. dbgrideh添加列、多表头及属性

    (一)动态添加列 procedure TForm2.FormCreate(Sender: TObject); var   vCol : TColumn; begin   vCol := DBGrid1 ...

  6. centos 安装ss

    yum install python-setuptools && easy_install pip pip install shadowsocks vi /etc/shadowsock ...

  7. JMeter之http接口测试

    能做哪些类型性能测试 接口 文件传输(ftp) 数据库 支持自定义java组件开发 安装 http://jmeter.apache.org/ 进入上面的链接 选择合适版本下载 启动 使用 Jmeter ...

  8. java 泛型的通配符和限定

    package cn.sasa.demo1; import java.util.ArrayList; import java.util.Collection; import java.util.Ite ...

  9. JSON.parseObject 和 JSON.toJSONString

    JSON.parseObject,是将Json字符串转化为相应的对象:JSON.toJSONString则是将对象转化为Json字符串.在前后台的传输过程中,Json字符串是相当常用的,这里就不多介绍 ...

  10. 前端 HTML body标签相关内容 常用标签 表单标签 form里面的 input标签介绍

    input标签用于接收用户输入.可以利用input 可以做登录页面 input标签是行内块标签 <input> 元素会根据不同的 type 属性,变化为多种形态. name属性:表单点击提 ...