Beauty Contest

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 38349   Accepted: 11851

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) 

题意:

给定 n 个点,求其中最远的两个点的距离的平方。

思路:

最远的两个点必定在凸包上,先求凸包,再用旋转卡壳求解。

总结:

旋转卡壳:现在凸包上找到一对点 Pi, Pj,Pj 是凸包上距 Pi 最远的点,则距离 Pi+1 (设Pi+1 在 Pi 的顺时针方向上的下一个点)最远的点必定在 Pj 的顺时针方向上(含 Pj);

在判断是否是最远距离上,通过两个相邻的点设为( Pi, Pi+1 ),在凸包上按顺(逆)时针方向遍历,查找一个 Pj+1 的距离小于 Pj 的距离,则 Pj 就是 Pi 的最远点、 Pj+1 是 Pi+1 的最远点。距离通过向量的叉乘,即 Pi, Pi+1, Pj(Pj+1)所围成的三角形面积判断。三角形底边长不变,面积越大则高越长,即顶点距点边两端点距离越远。

又求凸包时已经将凸包上的点按顺(逆)时针排列,据此,可在 O(n) 的时间内计算出凸包上所有点及与其相距最远的点。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 100005
#define eps 1e-8 using namespace std; struct point
{
double x, y;
point(){}
point(double a, double b):x(a), y(b){}
point operator-(point a){//向量减法
return point(x-a.x, y-a.y);
}
point operator+(point a){//向量加法
return point(x+a.x, y+a.y);
}
double operator*(point a){//向量叉积
return x*a.y-y*a.x;
}
bool operator<(const point a)const{
if(fabs(x-a.x)<eps)return y<a.y;//浮点数的判等不能直接用‘==’直接比较
return x<a.x;
}
bool operator==(const point a)const{
return (fabs(x-a.x)==eps && fabs(y-a.y));
}
double len(){//向量的模
return sqrt(x*x+y*y);
}
double len2(){//向量的模的平方
return (x*x+y*y);
}
}p[N], s[N];//p为点,s为栈 double cp(point a, point b, point o)//向量oa,ob叉积
{
return (a-o)*(b-o);
} void Convex(point *p, int &n)//Graham扫描法,栈内为所有凸包点
{
sort(p, p+n);
int top, m;
s[0] = p[0]; s[1] = p[1]; top = 1;
for(int i = 2; i < n; i++)//从前往后扫
{
while(top>0 && cp(p[i], s[top], s[top-1])>=0)top--;
s[++top] = p[i];
}
m = top;
s[++top] = p[n-2];
for(int i = n-3; i >= 0; i--)//从后往前扫
{
while(top>m && cp(p[i], s[top], s[top-1])>=0)top--;
s[++top] = p[i];
}
n = top;
} double rotating_calipers(point *ch,int n)//旋转卡壳
{
int q=1;
double ans=0;
ch[n]=ch[0];
for(int p=0;p<n;p++)
{
while(((ch[p+1]-ch[p])*(ch[q+1]-ch[p])) > ((ch[p+1]-ch[p])*(ch[q]-ch[p])))
q=(q+1)%n;
ans=max(ans,max((ch[p]-ch[q]).len2(),(ch[p+1]-ch[q+1]).len2()));//此题要求最远距离的平方
}
return ans;
} int main()
{
int n;
while(scanf("%d", &n)!=EOF && n)
{
for(int i = 0; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
sort(p, p+n);
int cnt=unique(p, p+n) - p;
Convex(p, cnt);
int ans = rotating_calipers(s, cnt);
printf("%d\n",ans);
}
return 0;
}

最新文章

  1. PHP常用函数整理
  2. @RequestMapping注解详解
  3. 【原/转】ios指令集以及基于指令集的app包压缩策略
  4. 5.9-2比较str1和str2截取后的子串
  5. Maven 安装以及一些开发技巧
  6. Careercup - Google面试题 - 6332750214725632
  7. dede织梦跨频道调用指定栏目文章的解决方法
  8. 《JavaScript高级程序设计》读书笔记 ---基本包装类型
  9. LogBack log出力路径
  10. JavaScript字符集编码与解码
  11. linux 如何打包代码
  12. Spring 加载静态资源
  13. linux任务前后台执行
  14. vs2010编译error_code
  15. windows7家庭版,专业版,旗舰版,企业版版本区别
  16. 【英文文档】 Installing Go from source Go语言官方编译指南 2019.02.27
  17. C++类的继承中构造函数和析构函数调用顺序例子
  18. 面试回顾——kafka
  19. js精度溢出解决方案
  20. windos 开启openssl

热门文章

  1. Cobalt Strike后渗透安装和初步使用
  2. PS01
  3. 计算几何(一):凸包问题(Convex Hull)
  4. E-Form++ for Windows CE源码库2020,嵌入式开放源码!
  5. kafk学习笔记(一)
  6. 学习Maven有感
  7. C语言普通写法实现:针对多次同步失败的节能处理机制
  8. 大话Python类语义
  9. GAN网络之入门教程(五)之基于条件cGAN动漫头像生成
  10. Android和。net加密。