poj2187 Beauty Contest (凸包 + 旋转卡壳)
Beauty Contest
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 38349 | Accepted: 11851 |
Description
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
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
2
Hint
题意:
思路:
总结:
旋转卡壳:现在凸包上找到一对点 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 (凸包 + 旋转卡壳)的更多相关文章
- POJ 2187 - Beauty Contest - [凸包+旋转卡壳法][凸包的直径]
题目链接:http://poj.org/problem?id=2187 Time Limit: 3000MS Memory Limit: 65536K Description Bessie, Farm ...
- POJ-2187 Beauty Contest,旋转卡壳求解平面最远点对!
凸包(旋转卡壳) 大概理解了凸包A了两道模板题之后在去吃饭的路上想了想什么叫旋转卡壳呢?回来无聊就搜了一下,结果发现其范围真广. 凸包: 凸包就是给定平面图上的一些点集(二维图包),然后求点集组成的 ...
- POJ2187 Beauty Contest (旋转卡壳算法 求直径)
POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...
- Beauty Contest 凸包+旋转卡壳法
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27507 Accepted: 8493 D ...
- POJ 2187 Beauty Contest [凸包 旋转卡壳]
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36113 Accepted: 11204 ...
- POJ2187 Beauty Contest(旋转卡壳)
嘟嘟嘟 旋转卡壳模板题. 首先求出凸包. 然后\(O(n ^ 2)\)的算法很好想,但那就不叫旋转卡壳了. 考虑优化:直观的想是在枚举点的时候,对于第二层循环用二分或者三分优化,但实际上两点距离是不满 ...
- 【POJ 2187】Beauty Contest 凸包+旋转卡壳
xuán zhuǎn qiǎ ké模板题 是这么读吧(≖ ‿ ≖)✧ 算法挺简单:找对踵点即可,顺便更新答案. #include<cstdio> #include<cstring&g ...
- POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- 【POJ】2187 Beauty Contest(旋转卡壳)
http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...
- [USACO2003][poj2187]Beauty Contest(凸包+旋转卡壳)
http://poj.org/problem?id=2187 题意:老题了,求平面内最远点对(让本渣默默想到了悲剧的AHOI2012……) 分析: nlogn的凸包+旋转卡壳 附:http://www ...
随机推荐
- Redis基础知识补充及持久化、备份介绍
Redis知识补充 在上一篇博客<Redis基础认识及常用命令使用(一)–技术流ken>中已经介绍了redis的一些基础知识,以及常用命令的使用,本篇博客将补充一些基础知识以及redis持 ...
- 《k8s权威指南》读书笔记
抽空读完了<k8s权威指南>一书,对k8s的总算有了较为系统的认知. 好记忆不如多写字,以下是读书笔记 第一章 k8s入门 k8s是什么: 一个开源的容器集群管理平台,可提供容器集群的自动 ...
- 最火的开源 IDE介绍与安装教程
导读:开发C/C++最好的IDE是什么,尤其对于很多初学者来说用什么IDE比较容易上手,本文将做以介绍,并为您演示如何下载与安装. 本文字数:1015,阅读时长大约:10分钟 (一)最火的开源IDE ...
- shiro-重写标签功能----shiro:hasPermission 标签重写
public abstract class ShiroAuthorizingRealm extends AuthorizingRealm{ private static final String OR ...
- 学会Git玩转GitHub(第一篇) 入门详解 - 精简归纳
学会Git玩转GitHub(第一篇) 入门详解 - 精简归纳 JERRY_Z. ~ 2020 / 9 / 25 转载请注明出处!️ 目录 学会Git玩转GitHub(第一篇) 入门详解 - 精简归纳 ...
- netty第一讲 创建
1.新建一个maven项目 https://blog.csdn.net/yanghaibobo110/article/details/73835469 2.netty是什么玩意 官方那个给出的介绍是 ...
- Centos-系统内存信息-free
free 显示系统内存信息,包括物理内存.虚拟内存.共享内存和系统缓存 相关选项 -b 以字节byte为单位显示内存使用情况 -k 以k为单位显示内存使用情况 -m 以MB为单位显示内存使用情况 - ...
- 一起构建Python生长土壤
环境 环境安装 1. 解释器 Python https://www.python.org/ 2. IDE Pycharm https://www.jetbrains.com/pycharm/downl ...
- Java学习day06
[方法] [可以在不是main的方法中调用其他方法] [方法调用时的参数问题] [方法调用] [上面定义了两个class,实际上不推荐] [递归调用] [方法的返回值] [retu ...
- Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例
本文实例讲述了Python数据结构与算法之图的最短路径(Dijkstra算法).分享给大家供大家参考,具体如下: # coding:utf-8 # Dijkstra算法--通过边实现松弛 # 指定一个 ...