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

题解:

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

 #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. git--分布式版本管理系统

    参考博客:廖雪峰的官方网站 一.window安装git Git官网直接下载安装程序,默认选项安装即可. 1.设置自己的git(cmd命令或者git bash进入) git config --globa ...

  2. laravels -- Swoole加速php

    LaravelS是一个胶水项目,用于快速集成Swoole到Laravel,然后赋予它们更好的性能.更多可能性. 环境 : ubuntu16 + nginx + php7.1 + LaravelS搭建高 ...

  3. JZOJ 1738. Heatwave

    Description 给你N个点的无向连通图,图中有M条边,第j条边的长度为: d_j. 现在有 K个询问. 每个询问的格式是:A B,表示询问从A点走到B点的所有路径中,最长的边最小值是多少? I ...

  4. css 自动换行,超出省略号代替

    overflow : hidden;    text-overflow: ellipsis;    display: -webkit-box;    -webkit-line-clamp: 2;    ...

  5. 在Ubuntu下安装gcc编译器+测试

    1.输入命令: sudo apt-get install gcc libc6-dev 2.创建文件hello.c使用命令: touch hello.c 3.在hello.c中写入:  #include ...

  6. No module named 'PyQt5.sip'

    使用pyinstaller打包python文件为windows可执行程序可能遇到的问题 pyinstaller yourprogram.py打包的程序双击打开一闪而过,提示上面标题的错误 把pycom ...

  7. 1014-31-首页12-显示weibo未读数--后台运行---定时器

    /** *  当app进入后台时调用 */- (void)applicationDidEnterBackground:(UIApplication *)application{    /**     ...

  8. 裸机——I2C

    网上搜了些资料,碍于智商和基础,看不懂, 只有将S5PV210 数据手册关于I2C的部分,翻译记录下,留到以后用. 1.OVERVIEW The S5PV210 RISC microprocessor ...

  9. ZOJ3640 概率DP

    Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at ...

  10. POJ:2739-Sum of Consecutive Prime Numbers(尺取)

    Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27853 Ac ...