LA 4728 旋转卡壳算法求凸包的最大直径
#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 旋转卡壳算法求凸包的最大直径的更多相关文章
- POJ2187 Beauty Contest (旋转卡壳算法 求直径)
POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...
- LA 4728 (旋转卡壳) Squares
题意: 求平面上的最远点对距离的平方. 分析: 对于这个数据量枚举肯定是要超时的. 首先这两个点一定是在凸包上的,所以可以枚举凸包上的点,因为凸包上的点要比原来的点会少很多,可最坏情况下的时间复杂度也 ...
- Gym - 101635K:Blowing Candles (简单旋转卡壳,求凸包宽度)
题意:给定N个点,用矩形将所有点覆盖,要求矩形宽度最小. 思路:裸体,旋转卡壳去rotate即可. 最远距离是点到点:宽度是点到边. #include<bits/stdc++.h> #de ...
- poj 2187 凸包加旋转卡壳算法
题目链接:http://poj.org/problem?id=2187 旋转卡壳算法:http://www.cppblog.com/staryjy/archive/2009/11/19/101412. ...
- POJ 2079 Triangle(凸包+旋转卡壳,求最大三角形面积)
Triangle Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 7625 Accepted: 2234 Descript ...
- POJ 3608 Bridge Across Islands(旋转卡壳,两凸包最短距离)
Bridge Across Islands Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7202 Accepted: ...
- BZOJ 1185: [HNOI2007]最小矩形覆盖-旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标-备忘板子
来源:旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标 BZOJ又崩了,直接贴一下人家的代码. 代码: #include"stdio.h" #include"str ...
- poj 3608(旋转卡壳求解两凸包之间的最短距离)
Bridge Across Islands Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9768 Accepted: ...
- POJ - 2187:Beauty Contest (最简单的旋转卡壳,求最远距离)
Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the ti ...
随机推荐
- openstack安装dashboard后访问horizon出错 End of script output before headers: django.wsgi
在配置文件中增加如下的一句解决问题 /etc/apache2/conf-available/openstack-dashboard.conf WSGIApplicationGroup %{GLOBAL ...
- python读取.mat文件
可以先看一下.mat中存了些什么: import scipy.io as sio box_file = '/home/bnrc/formatm/test/1479504458876408533_box ...
- JAVA中IP和整数相互转化(含有掩码的计算)
import java.net.InetAddress;/** * 用于IP和整数之间的相互转换 * @author Andy.Wang * */public class IPv4Util { ...
- 在行列都排好序的矩阵中找数 【题目】 给定一个有N*M的整型矩阵matrix和一个整数K, matrix的每一行和每一 列都是排好序的。实现一个函数,判断K 是否在matrix中。 例如: 0 1 2 5 2 3 4 7 4 4 4 8 5 7 7 9 如果K为7,返回true;如果K为6,返 回false。 【要求】 时间复杂度为O(N+M),额外空间复杂度为O(1)。
从对角考虑 package my_basic.class_3; /** * 从对角开始 */ public class Code_09_FindNumInSortedMatrix { public s ...
- eclips配置
新建空workspace import... configMathod:main:project:eFT-Debug@eFTSlnC/C++ Aplication /media/B/testspa2. ...
- jdk环境变量配置(参考自《疯狂java讲义》)
做个记录,免得每次配环境都要百度 环境变量的配置 path环境变量配置的作用:程序的执行需要使用外部指令javac, 但是javac指令仅仅能在JDK安装目录下的bin目录下使用,因此程序只能写入bi ...
- MySQL开启日志跟踪
在开发过程中有时候会遇到sql相关的问题,但是有时候代码中不会直接看到真实的sql,想要看到mysql中实际执行的是什么sql,可以通过开启日志跟踪方式查看. 1 开启日志跟踪 SET GLOBAL ...
- PHP获取文件后缀的7中方法
在日常的工作当中我们避免不了要经常获取文件的后缀名,今天我就整理了一下7种获取文件后缀的方法,希望对大家有所帮助. $url = 'http://www.baidu.com/uploads/20185 ...
- docker系列之基础命令-1
1.docker基础命令 docker images 显示镜像列表 docker ps 显示容器列表 docker run IMAGE_ID 指定镜像, 运行一个容器 docker start/sto ...
- PWA天气应用
https://codelabs.developers.google.com/codelabs/your-first-pwapp/#0 1.介绍 这里将使用PWA技术来构建一个天气web应用,这个ap ...