http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1393

http://poj.org/problem?id=2187 Beauty Contest

1393: Robert Hood

Description

Input

Output

Sample Input

5
-4 1
-100 0
0 4
2 -3
2 300

Sample Output

316.86590223

HINT

Source

分析:

给你 N 个点, 求所有点中最远两点距离。即是凸包直径。

凸包+旋转卡壳

AC代码:

 #include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = +;
int n,m; struct Point{
double x,y;
Point(){};
Point(double _x, double _y)
{
x = _x;
y = _y;
} Point operator - (const Point & B) const
{
return Point(x-B.x, y-B.y);
}
}p[maxn], ch[maxn]; bool cmp(Point p1, Point p2)
{
if(p1.x == p2.x) return p1.y < p2.y;
return p1.x < p2.x;
} int squarDist(Point A, Point B) /**距离的平方*/
{
return (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y);
} double Cross(Point A, Point B) /**叉积*/
{
return A.x*B.y-A.y*B.x;
} void ConvexHull() /** 基于水平的Andrew算法求凸包 */
{
sort(p,p+n,cmp); /**先按照 x 从小到大排序, 再按照 y 从小到大排序*/
m = ; for(int i = ; i < n; i++) /** 从前往后找 */
{
while(m > && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
int k = m;
for(int i = n-; i >= ; i--) /**从后往前找, 形成完整的封闭背包*/
{
while(m > k && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
if(n > ) m--;
} int rotating_calipers() /**旋转卡壳模板*/
{
int q = ;
int ans = ;
ch[m] = ch[]; /**凸包边界处理*/
for(int i = ; i < m; i++) /**依次用叉积找出凸包每一条边对应的最高点*/
{
while(Cross(ch[i+]-ch[i], ch[q+]-ch[i]) > Cross(ch[i+]-ch[i], ch[q]-ch[i]))
q = (q+)%m;
ans = max(ans, max(squarDist(ch[i], ch[q]), squarDist(ch[i+], ch[q+])));
}
return ans;
} int main()
{
while(scanf("%d", &n) != EOF)
{
if(n == ) break;
for(int i = ; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y); ConvexHull(); printf("%.8lf\n", sqrt(rotating_calipers()));
}
return ;
}

同学用结构体 + 遍历凸包也可以解决。

AC代码:

 #include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct point
{
int x;
int y;
}p[],res[];
int cmp(point p1,point p2)
{
return p1.y<p2.y||(p1.x==p2.x&&p1.x<p2.x);
}
bool ral(point p1,point p2,point p3)
{
return (p2.x-p1.x)*(p3.y-p1.y)>(p3.x-p1.x)*(p2.y-p1.y);
}
int main()
{
int n,i,j;
while((scanf("%d",&n))!=EOF)
{
for(i=;i<n;i++)
scanf("%d %d",&p[i].x,&p[i].y);
sort(p,p+n,cmp);
res[]=p[];
res[]=p[];
int top=;
for(i=;i<n;i++)
{
while(top&&!ral(res[top],res[top-],p[i]))
top--;
res[++top]=p[i];
}
int len=top;
res[++top]=p[n-];
for(i=n-;i>=;i--)
{
while(top!=len&&!ral(res[top],res[top-],p[i]))
top--;
res[++top]=p[i];
}
double maxx=;
for(i=;i<top;i++)
{
for(j=i+;j<top;j++)
{
double s=(res[i].x-res[j].x)*(res[i].x-res[j].x)+(res[i].y-res[j].y)*(res[i].y-res[j].y);
if(s>maxx)
maxx=s; }
}
printf("%.8lf\n",sqrt(maxx));
}
return ;
}

1393: Robert Hood 旋转卡壳 凸包的更多相关文章

  1. 【旋转卡壳+凸包】BZOJ1185:[HNOI2007]最小矩形覆盖

    1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 1945  Solve ...

  2. Gym 101606B - Breaking Biscuits - [凸包+旋转卡壳][凸包的宽度]

    题目链接:https://codeforces.com/gym/101606/problem/B 题解: 对于给出的 $n$ 个点,先求这些点的凸包,然后用旋转卡壳求出凸包的宽度(Width (min ...

  3. POJ 3608 Bridge Across Islands --凸包间距离,旋转卡壳

    题意: 给你两个凸包,求其最短距离. 解法: POJ 我真的是弄不懂了,也不说一声点就是按顺时针给出的,不用调整点顺序. 还是说数据水了,没出乱给点或给逆时针点的数据呢..我直接默认顺时针给的点居然A ...

  4. 【BZOJ 1069】【SCOI 2007】最大土地面积 凸包+旋转卡壳

    因为凸壳上对踵点的单调性所以旋转卡壳线性绕一圈就可以啦啦啦--- 先求凸包,然后旋转卡壳记录$sum1$和$sum2$,最后统计答案就可以了 #include<cmath> #includ ...

  5. 【POJ 2187】Beauty Contest(凸包直径、旋转卡壳)

    给定点集的最远两点的距离. 先用graham求凸包.旋(xuán)转(zhuàn)卡(qiǎ)壳(ké)求凸包直径. ps:旋转卡壳算法的典型运用 http://blog.csdn.net/hanch ...

  6. 【BZOJ-1069】最大土地面积 计算几何 + 凸包 + 旋转卡壳

    1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2707  Solved: 1053[Submit][Sta ...

  7. [USACO2003][poj2187]Beauty Contest(凸包+旋转卡壳)

    http://poj.org/problem?id=2187 题意:老题了,求平面内最远点对(让本渣默默想到了悲剧的AHOI2012……) 分析: nlogn的凸包+旋转卡壳 附:http://www ...

  8. 【BZOJ】1069: [SCOI2007]最大土地面积(凸包+旋转卡壳)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1069 显然这四个点在凸包上,然后枚举两个点找上下最大的三角形即可. 找三角形表示只想到三分QAQ.. ...

  9. hdu 3934&&poj 2079 (凸包+旋转卡壳+求最大三角形面积)

    链接:http://poj.org/problem?id=2079 Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissio ...

随机推荐

  1. onDestroy  和 onBackPressed、onfinish 的区别

    在android 开发中, 我容易不区分 onDestroy .onBackpress.onFinish. 其实,可以这样理解: (1)onDestory 的使用,是销毁了activity的实例在内存 ...

  2. Python for Infomatics 第12章 网络编程二(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.3 用HTTP协议获取一张 ...

  3. php递归获取顶级父类id

    php递归获取顶级父类id function get_top_parentid($id){ $r = M('navclass')->where('id = '.$id)->field('i ...

  4. About_php_封装函数

    <?php //编写数据库操作的魔术函数 function mysql_bind(){ //首先我们不知道外面会传入多少个参数 //可以用func_get_args()方法来获取全部传入参数,这 ...

  5. 通过Gulp使用Browsersync实现浏览器实时响应文件更改

    Gulp是什么鬼 Browsersync又是什么鬼 如何安装使用Browsersync 安装 使用 效果图 参考 Gulp是什么鬼 Gulp是一种基于node.js的构建工具,有关构建工具的概念请移步 ...

  6. Spring中scope作用域

    scope作用域: 1.prototype 2.request      3.session 4.singleton 5.global session 1.prototype(多例) prototyp ...

  7. JS仿淘宝星星评价

    //直接复制过去就可以了(你也可以吧css和js封装成css和js文件导入). <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transit ...

  8. http协议笔记

    协议:双方/多方共同遵守的一个规范.类比生活中协议 理解: webservice=http协议+xml Rest         =http协议+json 开始,客户端和其他服务器都是没有关系的,比如 ...

  9. php开发常见问题

    ajax 方面:   ajax写法:   //简写版ajax$.get('url.php',{'name':'myname','age':'18'},function(data){},'json'); ...

  10. mysql锁 实战测试代码

    存储引擎 支持的锁定 MyISAM 表级锁 MEMORY 表级锁 InnoDB 行级锁 BDB 页面锁 表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最高,并发度最低.行级锁:开销 ...