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;
}

poj2187 Beauty Contest (凸包 + 旋转卡壳)的更多相关文章

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

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

  2. POJ-2187 Beauty Contest,旋转卡壳求解平面最远点对!

     凸包(旋转卡壳) 大概理解了凸包A了两道模板题之后在去吃饭的路上想了想什么叫旋转卡壳呢?回来无聊就搜了一下,结果发现其范围真广. 凸包: 凸包就是给定平面图上的一些点集(二维图包),然后求点集组成的 ...

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

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

  4. Beauty Contest 凸包+旋转卡壳法

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27507   Accepted: 8493 D ...

  5. POJ 2187 Beauty Contest [凸包 旋转卡壳]

    Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36113   Accepted: 11204 ...

  6. POJ2187 Beauty Contest(旋转卡壳)

    嘟嘟嘟 旋转卡壳模板题. 首先求出凸包. 然后\(O(n ^ 2)\)的算法很好想,但那就不叫旋转卡壳了. 考虑优化:直观的想是在枚举点的时候,对于第二层循环用二分或者三分优化,但实际上两点距离是不满 ...

  7. 【POJ 2187】Beauty Contest 凸包+旋转卡壳

    xuán zhuǎn qiǎ ké模板题 是这么读吧(≖ ‿ ≖)✧ 算法挺简单:找对踵点即可,顺便更新答案. #include<cstdio> #include<cstring&g ...

  8. POJ 2187 Beauty Contest【旋转卡壳求凸包直径】

    链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  9. 【POJ】2187 Beauty Contest(旋转卡壳)

    http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...

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

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

随机推荐

  1. Linux实战(8):查看文件大小

    du -sh *

  2. SessionMiddleware源码分析

    settings.py文件中 MIDDLEWARE = [ 'django.contrib.sessions.middleware.SessionMiddleware', ] # from djang ...

  3. 计算"aabbc"中最多的相同字母数

    package Test; import java.util.HashMap; import java.util.Map; public class test3 { /** * 计算"aab ...

  4. 深入研究RocketMQ生产者发送消息的底层原理

    前言 hello,小伙伴们,王子又来和大家研究RocketMQ的原理了,之前的文章RocketMQ生产部署架构如何设计中,我们已经简单的聊过了生产者是如何发送消息给Broker的. 我们简单回顾一下这 ...

  5. Redis中的事务(多命令)操作

    作为一个nosql数据库,事务是必要功能.但是redis我们是可以理解为它不支持事务操作的,因为它的特征完全不满足我们对事物的正常理解 ps:我不知道是谁一开始提出redis支持事务的,但是我更倾向于 ...

  6. Oracle学习(九)存过、游标、RECORD整合

    一.代码 /*1.定义存过*/ CREATE OR REPLACE PROCEDURE PRO_RE_USER_ORD_GOODS( --NOW_DATE格式:YYYYMMDD 如:20190801( ...

  7. C# NX二次开发环境搭建

    在网上看到一篇C#二次开发环境搭建的文章:NX二次开发-使用NXOPEN C#手工搭建开发环境配置 ,写得非常好.我按照文章操作,过程中遇到几个问题,把问题分享给大家,希望对各位有帮助. 注意三点: ...

  8. Python3基础数据类型(数字、字符串、元组、列表、集合、字典)

    笔记参考了菜鸟教程 Python 中的变量不需要声明,赋值才创建.赋值给变量的是什么类型变量就是什么类型 多个变量赋值 1 a, b, c = 1, 2, "runoob" 标准数 ...

  9. java基础篇1

    JAVA基础篇1 注释 单行注释 //这是一个单行注释,由两个斜杠组成,不能嵌套多行注释 多行注释 /*这是一个 多行注释 ,//里面不能嵌套多行注释, 但是可以嵌套单行注释*/ 文档注释 /**ja ...

  10. day61:Linux:权限管理&rpm软件包管理&yum工具

    目录 1.权限管理 2.rpm软件包管理 3.yum工具(联网) 权限管理 1.什么是权限? 权限主要用来约束用户能对系统所做的操作 2.为什么要使用权限? 因为系统中不可能只存在一个root用户,一 ...