• 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
    ",ans);
      return 0;
    }
  • 相关阅读:
    关于素数的具体问题
    Scala Apply
    Scala内部类
    Scala 类和对象
    Scala Tuple类型
    Scala数组
    sql server 游标
    表变量和临时表详解
    子查询详解
    EXEC 和 SP_EXECUTESQL的区别
  • 原文地址:https://www.cnblogs.com/Narh/p/10143271.html
Copyright © 2020-2023  润新知