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 ...
随机推荐
- Fragmen和Activity之间的通信--接口和实现的分离(转)
Fragmen和Activity之间的通信--接口和实现的分离(转) 分类: Android平台 在平板的开发过程中通常都会采用多个Fragment的实现方式,通常有一个为list的Fragm ...
- LB 高可扩展性集群(负载均衡集群)
一.什么是负载均衡 首先我们先介绍一下什么是负载均衡: 负载平衡(Load balancing)是一种计算机网络技术,用来在多个计算机(计算机集群).网络连接.CPU.磁盘驱动器或其他资源中分配负载, ...
- Android Studio:Gradle DSL method not found: 'runProguard()'
Android Studio发布了新的1.0版,更新之后却发现原来在0.8下面正常的项目编译失败了,从报错上来看是卡在gradle上面. Gradle DSL method not found: 'r ...
- corosync集群的选举算法
<Cluster Concepts> http://linux-ha.org/wiki/Cluster_Concepts <Managing Computers with Autom ...
- 51nod1138(math)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1138 题意:中文题诶- 思路:假设 x=a1+(a1+1)+ ...
- Spring第一个例子的补充
1.首先导入需要的包: 2.文件结构: 3.先看xml配置文件: <?xml version="1.0" encoding="UTF-8"?> &l ...
- MySQL 索引的使用
一.or 的使用 (1)MySQL版本大于 5.x 的会使用 index merge 功能,即可以将多个单列索引集合起来使用,不过在查询时使用 or 的话,引擎为 myisam 的会开启 index ...
- android 点滴记录
1.AndroidM环境下,在framework层添加代码会对jar包的package name进行检查,并提示”unknown package name of class file”怎么解决? 产生 ...
- Servlet实现文件上传(简单)(一)
1..使用到的jar包,为apache的一个子项目 此commons-fileupload-1.2.2需要以下commons-io-2.0.1的支持 2.页面展示fileUpload.jsp ...
- CodeForces 622D Optimal Number Permutation
是一个简单构造题. 请观察公式: 绝对值里面的就是 |di-(n-i)|,即di与(n-i)的差值的绝对值. 事实上,对于任何n,我们都可以构造出来每一个i的di与(n-i)的差值为0. 换句话说,就 ...