bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670

用叉积判断。注意两端的平行于 y 轴的。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define db double
using namespace std;
const int N=5005,INF=1e7;
int n,sta[N],top;db ans;
struct Node{
  int x,y;
  Node(int x=0,int y=0):x(x),y(y) {}
  bool operator< (const Node &b)const
  {return x<b.x||(x==b.x&&y<b.y);}
  Node operator- (const Node &b)const
  {return Node(b.x-x,b.y-y);}
}a[N];
ll Sqr(int x){return (ll)x*x;}
ll Cross(Node a,Node b){return (ll)a.x*b.y-(ll)a.y*b.x;}
db dist(Node a,Node b){return sqrt(Sqr(a.x-b.x)+Sqr(a.y-b.y));}
int main()
{
  scanf("%d",&n);
  for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
  sort(a+1,a+n+1);
  for(int i=1;i<=n;i++)
    {
      while(top>1&&Cross(a[i]-a[sta[top]],a[i]-a[sta[top-1]])>=0)top--;
      sta[++top]=i;
    }
  for(int i=2;i<=top;i++)ans+=dist(a[sta[i-1]],a[sta[i]]);
  for(int i=1;i<=n;i++)a[i].y=INF-a[i].y+1;
  sort(a+1,a+n+1);
  top=0;
  for(int i=1;i<=n;i++)
    {
      while(top>1&&Cross(a[i]-a[sta[top]],a[i]-a[sta[top-1]])>=0)top--;
      sta[++top]=i;
    }
  for(int i=2;i<top;i++)ans+=dist(a[sta[i-1]],a[sta[i]]);
  if(top>1&&a[sta[top]].x!=a[sta[top-1]].x)ans+=dist(a[sta[top-1]],a[sta[top]]);
  if(a[1].x==a[2].x)ans+=abs(a[1].y-a[2].y);
  printf("%.2f\n",ans);
  return 0;
}

 

转载于:https://www.cnblogs.com/Narh/p/10143271.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值