poj 2187 Beauty Contest —— 旋转卡壳

题目:http://poj.org/problem?id=2187

学习资料:https://blog.csdn.net/wang_heng199/article/details/74477738

https://www.jianshu.com/p/74c25c0772d6

注意求凸包时先下后上,保持逆时针;

别忘了给点排序囧。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int const xn=50005;
int n,tot,ans;
struct P{
  int x,y;
  P(int x=0,int y=0):x(x),y(y) {}
  bool operator < (const P &b) const
  {return x<b.x||(x==b.x&&y<b.y);}
}p[xn],c[xn];
P operator - (const P &a,const P &b){return P(a.x-b.x,a.y-b.y);}
int cross(P a,P b){return a.x*b.y-a.y*b.x;}
int maxx(int x,int y){return x<y?y:x;}
int rd()
{
  int ret=0,f=1; char ch=getchar();
  while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}
  while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();
  return f?ret:-ret;
}
void find()
{
  sort(p+1,p+n+1);//!
  for(int i=1;i<=n;i++)//under
    {
      while(tot>1&&cross(c[tot]-c[tot-1],p[i]-c[tot])<=0)tot--;
      c[++tot]=p[i];
    }
  int num=tot;
  for(int i=n-1;i;i--)//top
    {
      while(tot>num&&cross(c[tot]-c[tot-1],p[i]-c[tot])<=0)tot--;
      c[++tot]=p[i];
    }
  tot--;//p[1]
}
int sqr(int x){return x*x;}
int disq(P a,P b){return sqr(a.x-b.x)+sqr(a.y-b.y);}
void work()
{
  ans=disq(c[1],c[2]); c[tot+1]=c[1];
  for(int i=1,p=2;i<=tot;i++)
    {
      while(cross(c[p]-c[i],c[p]-c[i+1])<cross(c[p+1]-c[i],c[p+1]-c[i+1]))
    {p++; if(p==tot+1)p=1;}
      ans=maxx(ans,maxx(disq(c[p],c[i]),disq(c[p+1],c[i+1])));
    }
}
int main()
{
  n=rd();
  for(int i=1;i<=n;i++)p[i].x=rd(),p[i].y=rd();
  find(); work();
  printf("%d\n",ans);
  return 0;
}

 

转载于:https://www.cnblogs.com/Zinn/p/10146273.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值