POJ 2187 Beauty Contest
来源:互联网 发布:新浪汽车销量数据库 编辑:程序博客网 时间:2024/06/04 13:40
旋转卡壳。
模板题,就不详细说做法了,找了一些材料,这篇写的很不错:http://www.cnblogs.com/xdruid/archive/2012/07/01/2572303.html
总的复杂度,凸包O(nlogn),旋转卡壳O(n),总O(nlogn)
还翻了一些别人的代码,发现他们在做旋转卡壳,最大三角形已经定下时,为了处理两边平行的情况,取了两次距离,分别是(ch[p],ch[q])和(ch[p+1],ch[q+1])?但是为什么我觉得这并没有必要。。。?因为对于最远点对,(称三角形在凸包上正在做的边为底边,与左边顶点相连的边称为左边)一定存在一个底边已经确定的三角形的左边(或右边)是最长点对距离,这个可以脑补最远点对的旋转卡壳的逆时针(顺时针)旋转,所以应该只要算一个距离就好了吧?(反正我只算一个距离AC了)
#include<cstdio>#include<algorithm>#define N 50005using namespace std;int n, q[N], top;struct Point{ int x, y;}p[50000]; bool cmp(Point a, Point b){ return a.x<b.x||(a.x==b.x&&a.y<b.y);}int cross(Point a, Point b, Point c){ return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}void graham(){ sort(p+1,p+n+1,cmp); top=-1; for(int i = 1; i <= n; i++) { while(top>0 && cross(p[q[top-1]],p[q[top]],p[i]) <= 0)top--; q[++top]=i; } int tmp=top; for(int i = n-1; i >= 1; i--) { while(top>tmp && cross(p[q[top-1]],p[q[top]],p[i]) <= 0)top--; q[++top]=i; }}int dis(Point a, Point b){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}int rotating_calipers(){ int ans=0; for(int i = 0, j = 1; i < top; i++) { while(cross(p[q[i]],p[q[i+1]],p[q[j+1]])>cross(p[q[i]],p[q[i+1]],p[q[j]])) j=(j+1)%top; ans=max(ans,dis(p[q[i]],p[q[j]]));//说的是这里 } return ans;}int main(){ scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d%d",&p[i].x,&p[i].y); graham(); int ans = rotating_calipers(); printf("%d\n",ans);}
1 0
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- poj 2187 Beauty Contest
- Poj 2187 Beauty Contest
- poj 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187 Beauty Contest
- POJ 2187--Beauty Contest
- poj 2187 Beauty Contest
- poj 2187 Beauty Contest
- 一道很有意思的java线程题
- POJ 2478 Farey Sequence 欧拉函数运用
- swift 中获取任意对象的类名称
- POJ 3090 Visible Lattice Points 欧拉函数运用
- 使用图片精灵优化前端 减少http请求
- POJ 2187 Beauty Contest
- Django入门教程
- 模态弹出扩展modalPopupExtender
- POJ 3070 Fibonacci 矩阵快速幂
- Epoll代码实例
- HDU 1575 Tr A 矩阵快速幂
- android使用Fragment实现底部菜单使用show()和hide()来切换以保持Fragment状态
- POJ 1061 青蛙的约会 拓展欧几里德
- java smtp.sina.com发送邮件解决及编码修正!