您的位置:首页 > 其它

POJ 2187 凸包旋转卡壳

2013-02-15 00:07 393 查看
题意:

求平面最远点对。输出最远距离的平方。

参考:

http://www.cnblogs.com/Booble/archive/2011/04/03/2004865.html

http://www.cppblog.com/staryjy/archive/2009/11/19/101412.html

View Code

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

#define N 50050

using namespace std;

struct PO
{
int x,y;
}p
;

int stk
,top,n;

inline bool cmp(const PO &a,const PO &b)
{
if(a.x==b.x) return a.y<b.y;
else return a.x<b.x;
}

inline int cross(const PO &o,const PO &a,const PO &b)
{
return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
}

inline int get_dis2(const PO &a,const PO &b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

inline void read()
{
for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
sort(p+1,p+1+n,cmp);
}

inline void graham()
{
sort(p+1,p+1+n,cmp);
top=0;
stk[++top]=1; stk[++top]=2;
for(int i=3;i<=n;i++)
{
while(top>=2&&cross(p[stk[top-1]],p[stk[top]],p[i])<=0) top--;
stk[++top]=i;
}
int num=top;
for(int i=n-1;i>=1;i--)
{
while(top>num&&cross(p[stk[top-1]],p[stk[top]],p[i])<=0) top--;
stk[++top]=i;
}
}

inline int rotating_calipers()
{
int ans=0,q=2;
for(int i=1;i<top;i++)
{
while(cross(p[stk[i+1]],p[stk[q+1]],p[stk[i]])>cross(p[stk[i+1]],p[stk[q]],p[stk[i]]))
{
q=(q+1)%top;
if(q==0) q++;
}
ans=max(ans,max(get_dis2(p[stk[i]],p[stk[q]]),get_dis2(p[stk[i+1]],p[stk[q+1]])));
}
return ans;
}

inline void go()
{
graham();
printf("%d\n",rotating_calipers());
}

int main()
{
while(scanf("%d",&n)!=EOF) read(),go();
return 0;
}


表示和dyf神牛讲的一点都不一样。。

目测用旋转法解会恶心死。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: