zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Python注释
    RSA算法知识
    Ubuntu 14.04安装QQ2012
    学习Linux的好网站
    Linux编程学习笔记 -- Process
    Python urllib2 模块学习笔记
    Django Tutorial 学习笔记
    Java学习笔记:语言基础
    Python中的正则表达式
    读书笔记:黑客与画家
  • 原文地址:https://www.cnblogs.com/Narh/p/10143271.html
Copyright © 2011-2022 走看看