Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 25256   Accepted: 7756

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

题意:给n个点的坐标,计算这些点两两距离最大的那个距离;

思路:如果直接枚举,肯定超。所以可以先形成凸包,距离最大的那两个端点一定是凸包中的点。所以形成凸包后再枚举就可以了。
   这里用了graham算法,
 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn = ;
int top,stack[maxn];
int n;
struct Point
{
double x;
double y;
} point[maxn]; double cross(const Point &a, const Point &b, const Point &c)//三个点的叉积,结果大于0说明bc的极角大于ac的极角,等于0说明共线;
{
return (a.x-c.x)*(b.y-c.y) - (a.y-c.y)*(b.x-c.x);
} double dis(const Point &a, const Point &b)
{
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
} int cmp(const Point &a, const Point &b)//对点排序找出最左最下的那个点作为point[0];
{
if(a.y == b.y)
return a.x < b.x;
return a.y < b.y;
} void Graham()
{
sort(point,point+n,cmp);
for(int i = ; i <= ; i++)
stack[i] = i;
top = ;
for(int i = ; i < n; i++)
{
while(top && cross(point[i],point[stack[top]],point[stack[top-]]) >= )
top--;
stack[++top] = i;
}
int count = top;
stack[++top] = n-;
for(int i = n-; i >= ; i--)
{
while(top != count && cross(point[i],point[stack[top]],point[stack[top-]])>=)
top--;
stack[++top] = i;
}
} int main()
{
while(~scanf("%d",&n))
{
for(int i = ; i < n; i++)
scanf("%lf %lf",&point[i].x,&point[i].y); Graham(); double ans = ,distance;
for(int i = ; i < top; i++)
{
for(int j = ; j < i; j++)
{
distance = dis(point[stack[i]],point[stack[j]]);
if(ans < distance)
ans = distance;
}
}
printf("%.0lf\n",ans);
}
return ;
}

这是经典的计算几何学问题,判断向量p1=(x1,y1)到p2=(x2,y2)是否做左转,只需要判断x1*y2-x2*y1的正负,如果结果为正,则从p1到p2做左转。也就是向量的叉积。

Graham算法是这样的

1.将各点排序(),为保证形成圈,把P0在次放在点表的尾部;

2.准备堆栈:建立堆栈S,栈指针设为t,将0、1、2三个点压入堆栈S;

3.对于下一个点i

   只要S[t-1]、S[t]、i不做左转

     就反复退栈;

   将i压入堆栈S

4.堆栈中的点即为所求凸包;

Beauty Contest(graham求凸包算法)的更多相关文章

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

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

  2. Graham Scan凸包算法

    获得凸包的算法可以算是计算几何中最基础的算法之一了.寻找凸包的算法有很多种,Graham Scan算法是一种十分简单高效的二维凸包算法,能够在O(nlogn)的时间内找到凸包. 首先介绍一下二维向量的 ...

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

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

  4. POJ 1113 Wall(Graham求凸包周长)

    题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 #include <stdi ...

  5. HDU 1392 Surround the Trees (Graham求凸包周长)

    题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 #include <stdio.h> #include < ...

  6. POJ2187 Beauty Contest (旋转卡壳算法 求直径)

    POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...

  7. Graham求凸包模板

    struct P { double x, y; P(, ):x(x), y(y) {} double add(double a, double b){ ; return a+b; } P operat ...

  8. (模板)graham扫描法、andrew算法求凸包

    凸包算法讲解:Click Here 题目链接:https://vjudge.net/problem/POJ-1113 题意:简化下题意即求凸包的周长+2×PI×r. 思路:用graham求凸包,模板是 ...

  9. nyoj-78-圈水池(Graham算法求凸包)

    题目链接 /* Name:nyoj-78-圈水池 Copyright: Author: Date: 2018/4/27 9:52:48 Description: Graham求凸包 zyj大佬的模板, ...

随机推荐

  1. jboss 7 as1 日志配置

    原文地址:https://docs.jboss.org/author/display/AS71/Logging+Configuration Overview The overall server lo ...

  2. 关于text-align无法居中的问题

    昨天项目,一直出现一个无法居中的问题,最后发现竟然是text-align的问题,才发现自己对text-align的理解还是不够透彻,于是在此再举例分析下. css中的元素一共有三类:块元素.行内块和内 ...

  3. 【IBM】Merlin 给 Java 平台带来了非阻塞 I/O

    Merlin 给 Java 平台带来了非阻塞 I/O 新增的功能大幅降低了线程开销 Java 技术平台早就应该提供非阻塞 I/O 机制了.幸运的是,Merlin(JDK 1.4)有一根几乎在各个场合都 ...

  4. 10.3 noip模拟试题

    希望[题目描述]网页浏览器者有后退与前进按钮,一种实现这两个功能的方式是用两个栈,“前进栈”.“后退栈”.这里你需要实现以下几个功能:BACK: 如果“后退栈”为空则忽略此命令. 否则将当前两面压入“ ...

  5. Orace数据库锁表的处理与总结<摘抄与总结二>

    当Oracle数据库发生TX锁等待时,如果不及时处理常常会引起Oracle数据库挂起,或导致死锁的发生,产生ORA-60的错误. TX锁等待的分析 Oracle数据库中一般使用行级锁. 当Oracle ...

  6. Xcode升后插件失效

    Xcode升后插件失效,与添加插件不小心点击Skip Bundle解决办法 字数267 阅读4731 评论1 喜欢12 今天升级了xcode到6.4 发现之前装的插件不能使用了.这里有一个解决的方案: ...

  7. Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)

    CoreLocation是iOS中一个提供设备定位的框架.通过这个框架可以实现定位处理,从而获取位置数据,比如经度.纬度.海拔信息等.   1,定位精度的设置 定位服务管理类CLLocationMan ...

  8. 2014年10月16号--for语句实例

    Console.WriteLine("一对小兔一个月之后长成大兔,再过一个月后生新的一对兔子,且两年之后有多少对兔子,就是三兔子幼兔,小兔,成兔"); Console.WriteL ...

  9. Ubuntu 12.04下解决Rhythmbox Music Player乱码问题

    1.打开终端输入如下信息: $ sudo gedit ~/.profile 2.在打开的文档末尾加上如下两句: export GST_ID3_TAG_ENCODING=GBK:UTF-8:GB1803 ...

  10. hdoj 1060

    代码: #include <stdio.h>#include <math.h> int main(){    int t;    while(scanf("%d&qu ...