给定平面上的一些散点集,求最远两点距离的平方值。

题解:

旋转卡壳求出凸包,然后根据单调性,求出最远两点的最大距离

 #pragma GCC optimize(2)
#pragma G++ optimize(2)
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstdio> #define eps 0.00000001
#define N 50007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,top;
double ans; double sqr(double x){return x*x;}
struct P
{
double x,y;
P(){}
P(double _x,double _y):x(_x),y(_y){}
friend P operator+(P a,P b){return P(a.x+b.x,a.y+b.y);}
friend P operator-(P a,P b){return P(a.x-b.x,a.y-b.y);}
friend double operator*(P a,P b){return a.x*b.y-a.y*b.x;}
friend double operator/(P a,P b){return a.x*b.x+a.y*b.y;}
friend bool operator==(P a,P b){return fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps;}
friend bool operator!=(P a,P b){return !(a==b);}
friend bool operator<(P a,P b)
{
if(fabs(a.y-b.y)<eps)return a.x<b.x;
return a.y<b.y;
}
friend double dis2(P a){return sqr(a.x)+sqr(a.y);}
friend void print(P a){printf("%.2lf %.2lf\n",a.x,a.y);}
}p[N],q[N]; bool cmp(P a,P b)
{
if(fabs((b-p[])*(a-p[]))<eps)return dis2(a-p[])<dis2(b-p[]);
return (a-p[])*(b-p[])>;//叉乘大于0,表示向左转,a的斜率更小。
}
void Graham()//选出凸包上的点。
{
for (int i=;i<=n;i++)
if(p[i]<p[])swap(p[i],p[]);
sort(p+,p+n+,cmp);
q[++top]=p[],q[++top]=p[];
for (int i=;i<=n;i++)
{
while((q[top]-q[top-])*(p[i]-q[top-])<eps&&top>)top--;//如果当前的点的斜率更小,就替换
q[++top]=p[i];
}
}
void RC()//求直径。
{
q[top+]=q[];//因为凸包是一个圈。
int now=;
for (int i=;i<=top;i++)
{
while((q[i+]-q[i])*(q[now]-q[i])<(q[i+]-q[i])*(q[now+]-q[i]))
{
now++;
if(now==top+)now=;
}
ans=max(ans,dis2(q[now]-q[i]));
}
}
int main()
{
n=read();
for (int i=;i<=n;i++)
p[i].x=read(),p[i].y=read();
Graham();
RC();
printf("%d",(int)ans);
}

POJ2187 旋转卡壳 求最长直径的更多相关文章

  1. poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方

    旋转卡壳求凸包的直径的平方 板子题 #include<cstdio> #include<vector> #include<cmath> #include<al ...

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

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

  3. POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]

    题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...

  4. POJ2187(旋转卡壳)

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35459   Accepted: 10978 ...

  5. [hdu5251]矩形面积 旋转卡壳求最小矩形覆盖

    旋转卡壳求最小矩形覆盖的模板题. 因为最小矩形必定与凸包的一条边平行,则枚举凸包的边,通过旋转卡壳的思想去找到其他3个点,构成矩形,求出最小面积即可. #include<cstdio> # ...

  6. POJ 2079 Triangle 旋转卡壳求最大三角形

    求点集中面积最大的三角形...显然这个三角形在凸包上... 但是旋转卡壳一般都是一个点卡另一个点...这种要求三角形的情况就要枚举底边的两个点 卡另一个点了... 随着底边点的递增, 最大点显然是在以 ...

  7. POJ 2187 Beauty Contest【旋转卡壳求凸包直径】

    链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  8. 「POJ-3608」Bridge Across Islands (旋转卡壳--求两凸包距离)

    题目链接 POJ-3608 Bridge Across Islands 题意 依次按逆时针方向给出凸包,在两个凸包小岛之间造桥,求最小距离. 题解 旋转卡壳的应用之一:求两凸包的最近距离. 找到凸包 ...

  9. bzoj1069: [SCOI2007]最大土地面积 凸包+旋转卡壳求最大四边形面积

    在某块平面土地上有N个点,你可以选择其中的任意四个点,将这片土地围起来,当然,你希望这四个点围成的多边形面积最大. 题解:先求出凸包,O(n)枚举旋转卡壳,O(n)枚举另一个点,求最大四边形面积 /* ...

随机推荐

  1. ES6笔记01-声明变量

    ES6只有六种声明变量的方法:var命令和function命令,let和const命令,import命令和class命令.所以,ES6一共有6种声明变量的方法. const声明一个只读的常量.一旦声明 ...

  2. java动态返回一个大对象里多个小对象map返回,el表达式用c:set拼接

    基于堆内存,先把map放到返回值里 Map _map=new HashMap(); modelAndView.addObject("pledgeInsurance",_map);/ ...

  3. python获取Excel数据

    Python中一般使用xlrd(excel read)来读取Excel文件,使用xlwt(excel write)来生成Excel文件(可以控制Excel中单元格的格式),需要注意的是,用xlrd读取 ...

  4. PyQuery网页解析库

    from pyquery import PyQuery as pq 字符串初始化: doc = pq(html) URL初始化:doc = pq(url = "···") 文件初始 ...

  5. P1282

    题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9, ...

  6. SpringMVC + MyBatis简单示例

    该项目基于Maven开发,该项目中包含了MyBatis自动创建表的功能,具体实现查阅MyBatis---自动创建表 源码下载 配置 maven支持pom.xml <project xmlns=& ...

  7. Atom使用插件精选(FE)

    [转]原文https://zhuanlan.zhihu.com/p/24753739?refer=AlenQi Atom琳琅满目的插件中,为前端coder推荐一些实用的插件. sync-setting ...

  8. linux centos7--linux和window共享文件(samba)

    这里以VMWARE与主控真机来做实现实现 由于SMB在centos中自带,所以,无需像网上说的样子,要这删除,那卸载,直接搜索是否存在SAMBA的安装文件 一 查询包是否存在 [root@localh ...

  9. spring boot 入门1-----如何使用@Value注解读取配置文件以及使用@ConfigrationProperties注解

    读取.yml文件属性值的方式 1)如何将配置文件中的属性值与字段匹配起来 @Value("${配置文件中属性的名}") 在application.yml文件中 server: po ...

  10. Hibernate SQL方言

    RDBMS 方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB2 OS3 ...