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

题解:

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

 #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. linux下载利器之curl和wget的区别

    linux下载利器-------curl和wget的区别 curl和wget基础功能有诸多重叠,如下载等. 在高级用途上的curl由于可自定义各种请求参数所以长于模拟web请求,用于测试网页交互(浏览 ...

  2. 学习Pytbon第十八篇,异常处理

    什么是异常? 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行. 一般情况下,在Python无法正常处理程序时就会发生一个异常. 异常是Python对象,表示一个错误. 当Pyth ...

  3. Too many parameters: expected 1, was given 2 Query: SELECT count(id) FROM `user` WHERE username = ?; Parameters: [org.apache.commons.dbutils.handlers.ScalarHandler@453da22c, [李明]]

    public Object getValue(String sql,Object... args) { Connection conn = null; Object obj= null; try { ...

  4. python协程和IO多路复用

     协程介绍                                                                                                ...

  5. CSS3实现带阴影的弹球

    实现div上下跳动时,底部阴影随着变化 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  6. 1 Mongodb安装

    1.NoSQL简介 NoSQL,全名Not Only SQL,指的是非关系型的数据库 随着访问量的上升,网站的数据库性能出现了问题,于是NoSQL被设计出来了 优点.缺点 优点 高扩展性 分布式计算 ...

  7. setBackgroundResource和setImageResource的区别

    setBackgroundResource是设置view的背景图片 setImageResource是设置ImageView的图片 对于一个imagevewButton来说,你既可以setBackgr ...

  8. Jsoncpp 编译

    1. linux下编译jsoncpp 从(http://jsoncpp.sourceforge.net/)下载源码包“jsoncpp-src-0.5.0.tar.gz”,解压后在其解压后目录中运行 $ ...

  9. atomic integer 实现

    public final int getAndAddInt(Object o, long offset, int delta) { int v; do { v = getIntVolatile(o, ...

  10. 使用自己的类来作为hashtable的主键

    import java.util.*; class Counter { } class Groundhog2 { int ghNumber; Groundhog2(int n) { ghNumber ...