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

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


 
  简单凸包应用。
  题意简单说就是求凸包的最长直径。给出N个点,要你求这N个点最长的距离平方值。
  看到这道题我们的直接思路应该是暴力枚举每个点对,求出最长距离,但由于这道题的数据规模是5w,直接枚举会超时。所以我们应该换一个思路,既然这道题要求最长距离,那么我们应该很容易想到凸包。因为凸包上的点一定是给定点集的最外围点,所以最长距离的两个点应该在凸包上。所以解题思路应该是:先求凸包,再枚举
  凸包模板
 struct Point{
double x,y;
};
double dis(Point p1,Point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double xmulti(Point p1,Point p2,Point p0) //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
void graham(Point p[],int n) //点集和点的个数
{
int pl[];
//找到纵坐标(y)最小的那个点,作第一个点
int t = ;
for(int i=;i<=n;i++)
if(p[i].y < p[t].y)
t = i;
pl[] = t;
//顺时针找到凸包点的顺序,记录在 int pl[]
int num = ; //凸包点的数量
do{ //已确定凸包上num个点
num++; //该确定第 num+1 个点了
t = pl[num-]+;
if(t>n) t = ;
for(int i=;i<=n;i++){ //核心代码。根据叉积确定凸包下一个点。
double x = xmulti(p[i],p[t],p[pl[num-]]);
if(x<) t = i;
}
pl[num] = t;
} while(pl[num]!=pl[]);
}

  本题代码

 #include <iostream>
using namespace std;
struct Point{
int x,y;
}p[];
int xmulti(Point p1,Point p2,Point p0) //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int graham(Point p[],int n) //点集和点的个数
{
int pl[];
//找到纵坐标(y)最小的那个点,作第一个点
int t = ;
for(int i=;i<=n;i++)
if(p[i].y < p[t].y)
t = i;
pl[] = t;
//顺时针找到凸包点的顺序,记录在 int pl[]
int num = ; //凸包点的数量
do{ //已确定凸包上num个点
num++; //该确定第 num+1 个点了
t = pl[num-]+;
if(t>n) t = ;
for(int i=;i<=n;i++){ //核心代码。根据叉积确定凸包下一个点。
int x = xmulti(p[i],p[t],p[pl[num-]]);
if(x<) t = i;
}
pl[num] = t;
} while(pl[num]!=pl[]);
//计算最长距离
int Max = ;
for(int i=;i<num-;i++)
for(int j=i+;j<=num-;j++){
int tt = (p[pl[i]].x-p[pl[j]].x)*(p[pl[i]].x-p[pl[j]].x) + (p[pl[i]].y-p[pl[j]].y)*(p[pl[i]].y-p[pl[j]].y);
//注意这里要用p[pl[i]],不能是p[i],否则会WA
if(tt>Max)
Max = tt;
}
return Max;
}
int main()
{
int n;
while(cin>>n){
for(int i=;i<=n;i++) //输入n个点
cin>>p[i].x>>p[i].y;
cout<<graham(p,n)<<endl; //输出最长距离
}
return ;
}

类似题目:hdu 1348:Wall(计算几何,求凸包周长)

Freecode : www.cnblogs.com/yym2013

poj 2187:Beauty Contest(计算几何,求凸包,最远点对)的更多相关文章

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

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

  2. poj 2187 Beauty Contest(二维凸包旋转卡壳)

    D - Beauty Contest Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  3. POJ 2187 Beauty Contest ——计算几何

    貌似直接求出凸包之后$O(n^2)$暴力就可以了? #include <cstdio> #include <cstring> #include <string> # ...

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

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

  5. ●POJ 2187 Beauty Contest

    题链: http://poj.org/problem?id=2187 题解: 计算几何,凸包,旋转卡壳 一个求凸包直径的裸题,旋转卡壳入门用的. 代码: #include<cmath> # ...

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

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

  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 , 旋转卡壳求凸包的直径的平方

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

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

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

随机推荐

  1. ant-design getFieldDecorator 无法获取自定义组件的值

    1.自定义或第三方的表单控件,也可以与 Form 组件一起使用.只要该组件遵循以下的约定: (1)提供受控属性 value 或其它与 valuePropName 的值同名的属性. (2)提供 onCh ...

  2. 安卓Camera APP

    一.Camera package android.hardware            该类用于设定图像捕获设置,开启/关闭预览,抓拍图片以及获取帧用于编码视频.这个类是Camera服务的客户端,用 ...

  3. mac下在eclipse中怎样清除/切换svn

    1.打开终端,即用户的根文件夹(用户的home文件夹) 进入.subversion下的auth文件夹 localhost:auth brj$ pwd /Users/brj/.subversion/au ...

  4. 【VBA编程】03.判断输入年份是否是闰年

    通过输入月份,判断是否是闰年 [代码区域] Sub 判断闰年() Dim year As Integer '用于保存输入的年份 year = CInt(InputBox("请输入需要判断的年 ...

  5. sql中判断某个字符串是否包含一个字符串

    如果想从SQL Server中查询包含某个关键字的东东,怎么查询呢? 一般有两个方法: 1.用like——select * from tablename where field1 like like ...

  6. ASP.NET 推荐书籍

    ASP.NET 推荐书籍 1.首先推荐一本ASP.NET MVC的书籍 —— <Web开发新体验:ASP.NET 3.5 MVC架构与实战> [点评]:ASP.NET的MVC的书籍本来就不 ...

  7. 【原创】说说JSON和JSONP,也许你会豁然开朗,含jQuery用例

    说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域可以通过服 ...

  8. Incorrect integer value: '' for column 'mid' at row 11366Incorrect integer value: '' for column 'mid' at row 1,自增字段为空,添加记录时出错

    在本地机器做测试时,数据可以正常添加,但是将代码放置到服务器上时,提示:Incorrect integer value: '' for column 'mid' at row 11366Incorre ...

  9. 补习知识:Entity Framework Code First属性映射约定

    Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...

  10. 统一修改 UINavigationBar backItem

    { UINavigationBar * navigationBar = [UINavigationBar appearance]; //返回按钮的箭头颜色 [navigationBar setTint ...