LA4728 Squares
题意
就是求凸包点集的直径。
题解
当然选择旋转卡壳。
然后是实现上的技巧:
当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
画个图就能发现,这样找的是对于一条边三角形的最大高。为什么这样是对的呢?
凸包上一个点到其他点的距离是一个凸函数。然后在两条直线慢慢旋转的过程中,可以考虑直接转一条边,这样求出的是到这条直线的最大距离,显然就是对踵点对。
实现的时候初始化可以直接暴力转,因为均摊是\(O(n)\)的。时间复杂度\(O(T n \log n)\)。
由于坐标都是整数,而又不涉及需要实数运算的操作,所以`Point`类可以直接实现为整数坐标。
```cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rg register
#define il inline
#define co const
templateil T read()
{
rg T data=0;
rg int w=1;
rg char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
{
data=data*10+ch-'0';
ch=getchar();
}
return data*w;
}
templateT read(T&x)
{
return x=read();
}
using namespace std;
typedef long long ll;
struct Point
{
int x,y;
Point(int x=0,int y=0)
:x(x),y(y){}
bool operator<(co Point&rhs)co
{
return x<rhs.x||(x==rhs.x&&y<rhs.y);
}
bool operator==(co Point&rhs)co
{
return x==rhs.x&&y==rhs.y;
}
};
typedef Point Vector;
Vector operator-(co Point&A,co Point&B)
{
return Vector(A.x-B.x,A.y-B.y);
}
int Cross(co Vector&A,co Vector&B)
{
return A.xB.y-A.yB.x;
}
int Dot(co Vector&A,co Vector&B)
{
return A.xB.x+A.yB.y;
}
int Dist2(co Vector&A,co Vector&B)
{
return (A.x-B.x)(A.x-B.x)+(A.y-B.y)(A.y-B.y);
}
vectorConvexHull(vector&p)
{
sort(p.begin(),p.end());
p.erase(unique(p.begin(),p.end()),p.end());
int n=p.size();
int 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-2])<=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-2])<=0)
--m;
ch[m++]=p[i];
}
if(n>1)
--m;
ch.resize(m);
return ch;
}
int Diameter2(vector&points)
{
vectorp=ConvexHull(points);
int n=p.size();
if(n1)
return 0;
if(n2)
return Dist2(p[0],p[1]);
p.push_back(p[0]); // avoid %
int ans=0;
for(int u=0,v=1;u<n;++u)
{
for(;
LA4728 Squares的更多相关文章
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- 卡通图像变形算法(Moving Least Squares)附源码
本文介绍一种利用移动最小二乘法来实现图像变形的方法,该方法由用户指定图像中的控制点,并通过拖拽控制点来驱动图像变形.假设p为原图像中控制点的位置,q为拖拽后控制点的位置,我们利用移动最小二乘法来为原图 ...
- Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- [LintCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- HDU 1264 Counting Squares(线段树求面积的并)
Counting Squares Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- RSS(Residual Sum of Squares)的自由度为什么是n-1呢
[转载请注明出处]http://www.cnblogs.com/mashiqi 在回归问题中,偶尔我们会遇到求方差的估计的情况.举了例子,我们常常通过Gaussian分布${\cal N}(\mu , ...
- poj-3739. Special Squares(二维前缀和)
题目链接: I. Special Squares There are some points and lines parellel to x-axis or y-axis on the plane. ...
- [CareerCup] 7.5 A Line Cut Two Squares in Half 平均分割两个正方形的直线
7.5 Given two squares on a two-dimensional plane, find a line that would cut these two squares in ha ...
- POJ 2002 Squares
二分.... Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 14530 Accepted: 5488 Descr ...
随机推荐
- MySQL数据库中tinyint类型字段读取数据为true和false (MySQL的boolean和tinyint(1))
数据库一个表中有一个tinyint类型的字段,值为0或者1,如果取出来的话,0会变成false,1会变成true. MySQL保存boolean值时用1代表TRUE,0代表FALSE.boolean在 ...
- VMware Workstation 12 增加磁盘容量 Windows Server 2012 系统 扩展
1.安装虚拟机后,检查C盘容量大小,发现C盘现在的空间是59.9GB,如下图: 2.使用window+R键,出现运行窗口,输入‘cmd’——>‘cd C:\Program Files (x86) ...
- springmvc跨域
//mvc默认是text/plain;charset=ISO-8859-1@RequestMapping(value = "/xxx", produces = "appl ...
- java 判断对象的所有属性是否为空解决方案
public static boolean allfieldIsNUll(Object o){ try{ for(Field field:o.getClass().getDeclaredFields( ...
- LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
- Django1.11 的serializers序列化model
开发环境:Django1.11 Python3 如果想把很多model转化成json格式可以直接这样写: from django.views.generic.base import View fro ...
- JT809 加密解密算法
平台对接标准上说“在数据包发送之前,二进制数据包与伪随机序列按字节进行异或运算.加密算法如下:用N模伪随机序列发生器产生伪随机字节序列.将待传输的数据与伪随机码按字节进行异或运算”下面代码是C语言的代 ...
- 一个较好的style与ControlTemplate结合的示例(以Button为例)
<!--按钮背景画刷--> <LinearGradientBrush x:Key="buttonBackgroundBrush"> &l ...
- web项目在iis配置好后不能正确访问问题集锦,以及IIS常规设置
6.IIS配置好,项目无法访问,注意项目对应的应用程序池的net版本是否正确,是否集成(一般都是集成,很少是经典) 本项目用的4.0,可IIS默认程序池为2.0,将2.0改为4.0就行. 7.HT ...
- JDBC相关总结
JDBC statement的相关总结 1.Statement.PreparedStatement和CallableStatement都是接口(interface). 2.Statement继承自Wr ...