Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 24431   Accepted: 7459

Description

Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates. 
Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms. 

Input

* Line 1: A single integer, N 
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm 

Output

* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other. 

Sample Input

4
0 0
0 1
1 1
1 0

Sample Output

2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 

Source

 
 
 

旋转卡壳学习链接:

http://blog.csdn.net/ACMaker

 
 
 
想法:

总结起来,问题解决步骤为:

1、用Graham's Scanning求凸包

2、用Rotating Calipers求凸包直径,也就找到了最远点对。

该算法的平均复杂度为O(nlogn) 。最坏的情况下,如果这n个点本身就构成了一个凸包,时间复杂度为O(n^2)。 旋转卡壳可以用于求凸包的直径、宽度,两个不相交凸包间的最大距离和最小距离等。虽然算法的思想不难理解,但是实现起来真的很容易让人“卡壳”。

逆向思考,如果qa,qb是凸包上最远两点,必然可以分别过qa,qb画出一对平行线。通过旋转这对平行线,我们可以让它和凸包上的一条边重合,如图中蓝色直线,可以注意到,qa是凸包上离p和qb所在直线最远的点。于是我们的思路就是枚举凸包上的所有边,对每一条边找出凸包上离该边最远的顶点,计算这个顶点到该边两个端点的距离,并记录最大的值。直观上这是一个O(n2)的算法,和直接枚举任意两个顶点一样了。但是注意到当我们逆时针枚举边的时候,最远点的变化也是逆时针的,这样就可以不用从头计算最远点,而可以紧接着上一次的最远点继续计算,于是我们得到了O(n)的算法。

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std; struct Point
{
int x,y;
Point(int _x = , int _y = )
{
x = _x;
y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x, y - b.y);
}
int operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
int operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
void input()
{
scanf("%d%d",&x,&y);
}
};
int dist2(Point a,Point b)//距离的平方!!!attention!!
{
return (a-b)*(a-b);
}
const int MAXN = ;
Point list[MAXN];
int Stack[MAXN],top;
bool _cmp(Point p1,Point p2)
{
int tmp = (p1-list[])^(p2-list[]);
if(tmp > )return true;
else if(tmp == && dist2(p1,list[]) <= dist2(p2,list[]))
return true;
else return false;
}
void Graham(int n)//求凸包
{
Point p0;
int k = ;
p0 = list[];
for(int i = ;i < n;i++)
if(p0.y > list[i].y || (p0.y == list[i].y && p0.x > list[i].x))
{
p0 = list[i];
k = i;
}
swap(list[],list[k]);
sort(list+,list+n,_cmp);
if(n == )
{
top = ;
Stack[] = ;
return;
}
if(n == )
{
top = ;
Stack[] = ;
Stack[] = ;
return;
}
Stack[] = ;
Stack[] = ;
top = ;
for(int i = ;i < n;i++)
{
while(top > && ((list[Stack[top-]]-list[Stack[top-]])^(list[i]-list[Stack[top-]])) <= )
top--;
Stack[top++] = i;
}
} //旋转卡壳,求两点间距离平方的最大值
int rotating_calipers(Point p[],int n)
{
int ans = ;
Point v;
int cur = ;
for(int i = ;i < n;i++)
{
v = p[i]-p[(i+)%n];
while((v^(p[(cur+)%n]-p[cur])) < )//寻找到国定边的最远的点 p[cur],然后更新距离!!!
cur = (cur+)%n; ans = max(ans,max(dist2(p[i],p[cur]),dist2(p[(i+)%n],p[(cur+)%n])));//这里其实可以记录最远点对的坐标!!
}
return ans;
}
Point p[MAXN]; int main()
{
int n;
while(scanf("%d",&n) == )
{
for(int i = ;i < n;i++)
list[i].input();
Graham(n);
for(int i = ;i < top;i++)
{
p[i] = list[Stack[i]];
}
printf("%d\n",rotating_calipers(p,top)) ;
}
return ;
}
 
 

poj 2187 Beauty Contest(平面最远点)的更多相关文章

  1. POJ - 2187 Beauty Contest(最远点对)

    http://poj.org/problem?id=2187 题意 给n个坐标,求最远点对的距离平方值. 分析 模板题,旋转卡壳求求两点间距离平方的最大值. #include<iostream& ...

  2. poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)

    /* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...

  3. poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)

    链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...

  4. POJ 2187 Beauty Contest (求最远点对,凸包+旋转卡壳)

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 24283   Accepted: 7420 D ...

  5. poj 2187:Beauty Contest(计算几何,求凸包,最远点对)

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 26180   Accepted: 8081 D ...

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

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

  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 2187:Beauty Contest(旋转卡壳)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 32708   Accepted: 10156 Description Bes ...

  9. poj 2187 Beauty Contest

    Beauty Contest 题意:给你一个数据范围在2~5e4范围内的横纵坐标在-1e4~1e4的点,问你任意两点之间的距离的最大值的平方等于多少? 一道卡壳凸包的模板题,也是第一次写计算几何的题, ...

随机推荐

  1. 设计模式学习笔记——Visitor 访问者模式

    1.定义IVisitor接口,确定变化所涉及的方法 2.封装变化类.实现IVisitor接口 3.在实体类的变化方法中传入IVisitor接口,由接口确定使用哪一种变化来实现(封装变化) 4.在使用时 ...

  2. linux/Unix下的vim/vi指令的使用方法

    概述 以下这篇文章介绍的是关于vim的使用方法,由于我本身对linux没有太多的研究,写下的这篇文章纯属是在实际中经常使用vim指令,想通过这篇文章记录下来,方便以后使用时查找方便.个人认为,对于普通 ...

  3. ionic打包app——以安卓版本为例 辛苦之路~~~

    最近同事做了个angular项目,因为要离职,所以项目我就来接手了,用ionic打包app,然后无数配置的坑就等着我了~~~ 环境安装 1.nodejs 因为自己刚接触做angular项目,就更新到了 ...

  4. oracle常用sql语句和函数

    --查询表的字段数 select count(*) from user_tab_columns where table_name = '表名'; --查询数据库用户密码的profile(一般为defa ...

  5. C# 内存建表备忘

    #region=====建表===== DataSet dataSet; // 创建表 DataTable table = new DataTable("testTable"); ...

  6. QTP 11 补丁大全

    原文: http://relevantcodes.com/qtp-11-0-patches/ Patch Link Details Support for Chrome 19 QTPWEB_00102 ...

  7. tensorflow 关于 矩阵 运算 + 符号得含义。 2维 数组 + 1纬数组, 就是每一行都 加一边 1纬数组。 呵呵

    小锋子Shawn(403568338)  13:51:23mnist.training.images?墨须(964489899)  13:51:27我的图片是100*100的,该怎么兼容.  小锋子S ...

  8. 新手解惑:nginx&php-fpm&fastcgi 是什么关系

    首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者.   web server(比如说nginx)只是内容的分发者.比如,如果请求/index ...

  9. window.open弹窗阻止问题解决之道

    https://segmentfault.com/a/1190000015381923https://segmentfault.com/a/1190000014988094https://www.cn ...

  10. 【ABAP系列】SAP 销售订单的行项目里条件的增强

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 销售订单的行项目里条件的 ...