POJ2187(旋转卡壳)
Beauty Contest
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 35459 | Accepted: 10978 |
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
//2016.10.2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 50005
#define eps 1e-8 using namespace std; int n; 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);
}
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;
}
double len2(){//向量模的平方
return x*x+y*y;
}
}p[N]; struct polygon
{
int n;
point p[N];
}pg; double cp(point o, point a, point b)//向量oa,ob叉积
{
return (a-o)*(b-o);
} void Convex(int &n)//Graham扫描法
{
sort(p, p+n);
int top, m;
pg.p[] = p[]; pg.p[] = p[]; top = ;
for(int i = ; i < n; i++)//从前往后扫
{
while(top> && cp(p[i], pg.p[top], pg.p[top-])>=)top--;
pg.p[++top] = p[i];
}
m = top;
pg.p[++top] = p[n-];
for(int i = n-; i >= ; i--)//从后往前扫
{
while(top>m && cp(p[i], pg.p[top], pg.p[top-])>=)top--;
pg.p[++top] = p[i];
}
pg.n = top;
} int rotating_calipers()//旋转卡壳
{
int v = ;n = pg.n;
double ans = ;
pg.p[n] = pg.p[];
for(int u = ; u < n; u++)//旋转
{
while(cp(pg.p[u],pg.p[u+],pg.p[v+])>cp(pg.p[u],pg.p[u+],pg.p[v]))v = (v+)%n;
ans = max(ans, max((pg.p[u]-pg.p[v]).len2(), (pg.p[u+]-pg.p[v+]).len2()));
}
return ans;
} int main()
{
int n;
while(scanf("%d", &n)!=EOF && n)
{
for(int i = ; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
Convex(n);
int ans = rotating_calipers();
printf("%d\n", ans);
} return ;
}
POJ2187(旋转卡壳)的更多相关文章
- POJ2187 旋转卡壳 求最长直径
给定平面上的一些散点集,求最远两点距离的平方值. 题解: 旋转卡壳求出凸包,然后根据单调性,求出最远两点的最大距离 #pragma GCC optimize(2) #pragma G++ optimi ...
- POJ2187 Beauty Contest (旋转卡壳算法 求直径)
POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...
- poj2187 Beauty Contest(旋转卡壳)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Beauty Contest Time Limit: 3000MS Memor ...
- [POJ2187][BZOJ1069]旋转卡壳
旋转卡壳 到现在依然不确定要怎么读... 以最远点对问题为例,枚举凸包上的两个点是最简单的想法,时间复杂度O(n2) 我们想象用两条平行线卡着这个凸包,当其中一个向某个方向旋转的时候另一个显然也是朝同 ...
- [USACO2003][poj2187]Beauty Contest(凸包+旋转卡壳)
http://poj.org/problem?id=2187 题意:老题了,求平面内最远点对(让本渣默默想到了悲剧的AHOI2012……) 分析: nlogn的凸包+旋转卡壳 附:http://www ...
- POJ2187 Beauty Contest(旋转卡壳)
嘟嘟嘟 旋转卡壳模板题. 首先求出凸包. 然后\(O(n ^ 2)\)的算法很好想,但那就不叫旋转卡壳了. 考虑优化:直观的想是在枚举点的时候,对于第二层循环用二分或者三分优化,但实际上两点距离是不满 ...
- POJ2187(凸包+旋转卡壳)
这道题目的大意是给出一组二维空间的顶点,计算其中距离最远的两个顶点之间的距离. 先说明凸包的概念和求法. 定义:对于多边形P,若将P中任意的两个点(包含边上)用一条线段连接,线段都落于该多边形中(含边 ...
- POJ-2187 Beauty Contest,旋转卡壳求解平面最远点对!
凸包(旋转卡壳) 大概理解了凸包A了两道模板题之后在去吃饭的路上想了想什么叫旋转卡壳呢?回来无聊就搜了一下,结果发现其范围真广. 凸包: 凸包就是给定平面图上的一些点集(二维图包),然后求点集组成的 ...
- 算法复习——凸包加旋转卡壳(poj2187)
题目: Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest ...
随机推荐
- 64位系统访问注册表SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
public int ChecNonkWoW64() { try { ; string subKey = @"SOFTWARE\Microsoft\Windows\CurrentVersio ...
- Nexus搭建私服 学习
为什么要搭建nexus私服 因为有些公司不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以,有必要再局域网里找一台有外网权限的机器.搭建nexus私服,然后开发人员连接到这台私服 ...
- log4cxx入门第一篇--一个小例子
先看官网:http://logging.apache.org/log4cxx/index.html 转载自:http://wenku.baidu.com/view/d88ab5a9d1f34693da ...
- hibernate--一对多 多对一 双向关联 (重点!!!)
一方 group: package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax. ...
- MRC的下setter访问器的两种形式
// Person复合了Phone和Room // 第一种:比较合理 先判断对象形参传递的对象和原属性是否一致,不一致在释放旧值,给形参传递的值retain,因为retain方法会返回该对象,因此可以 ...
- Quick Cocos2dx MVC初步
今天看到了自己之前两年前写的一个地图编辑器, 写了不到一半就放弃了, 但是还是github上的小伙伴fork了, 真的感觉对不起那位伙计, 同时也鄙视一下一直以来懒得要死的自己, 希望这个demo不要 ...
- 23、手把手教你Extjs5(二十三)模块Form的自定义的设计[2]
在本节中将要加入各种类型的字段,在加入字段的时候由于可以一行加入多个字段,因此层次结构又多了一层fieldcontainer.form里面的主要层次结构如下: form -- fieldSet -- ...
- 内层div的margin-top影响外层div——引出外边距合并Collapsing margins
内层div的margin-top影响外层div——引出外边距合并Collapsing margins 作者:zccst 今天才算是了解边距合并.正如一位前辈所言,每一个CSS的坑,都让你学到不少知识. ...
- STM8S STM8L引脚如何配置最低(转)
源:STM8S STM8L引脚如何配置功耗最低 STM8S无任何外围电路 单片机CAP接104电容 复位接上拉电阻,其它引脚全部悬空,利用以下程序测试电流如下:(以前也用STM8L做过类似实验,情况也 ...
- S3C2440的SPI解析
位串行数据的频率.如果只希望发送,则接收数据可以保持伪位(dummy).此外如果只希望接收,则需要发送伪位'1'数据 使用SPI主要需要以下寄存器 选择SPI模式,中断模式,查询模式等SCK选择,主从 ...