zoukankan      html  css  js  c++  java
  • [COGS896] 圈奶牛

    描述

    农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏。他建造的围栏必须包括他的奶牛喜欢吃草的所有地点。对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度。

    INPUT FORMAT(file fc.in)

    输入数据的第一行包括一个整数 N。N(0 <= N <= 10,000)表示农夫约翰想要围住的放牧点的数目。接下来N 行,每行由两个实数组成,Xi 和 Yi,对应平面上的放牧点坐标(-1,000,000 <= Xi,Yi <= 1,000,000)。数字用小数表示。

    OUTPUT FORMAT(file fc.out)

    输出必须包括一个实数,表示必须的围栏的长度。答案保留两位小数。

    SAMPLE INPUT (file fc.in)

    4
    4 8
    4 12
    5 9.3
    7 8 

    SAMPLE OUTPUT (file fc.out)

    12.00

    思路

    凸包Graham扫描算法;

    Graham扫描算法的基本操作;

    选择一个最左侧最下方的点,作为基本点;

    将其余所有点按照与x轴夹角由小到大排序;

    从基本点开始连接凸包定点;

    如果pi在pi-1pi-2左侧,加入pi;

    如果在右侧或直线上,删除pi-1…,直到满足上面要求;

    函数们;

    Dis 求a,b两点间距离;

    Mul 比较p1p0,p2p0与x轴夹角的大小;

    Graham 求凸包;

    变量们;

    top 凸包顶点数;

    p 输入节点们;

    s 凸包顶点们;

    ans 凸包周长;

    代码实现

     1 #include<cmath>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 const int maxn=1e4+1;
     6 const int size=1e6+1;
     7 int n,top=2;
     8 double ans;
     9 struct data{double x,y;}p[maxn],s[maxn];
    10 inline double dis(data a,data b){
    11     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    12 }
    13 inline double mul(data p1,data p2,data p0){
    14     return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
    15 }
    16 inline bool cmp(data a,data b){
    17     if(mul(a,b,p[0])) return mul(a,b,p[0])>0;
    18     else return dis(a,p[0])<dis(b,p[0]);
    19 }
    20 void graham(){
    21     data t;
    22     int k=0;
    23     for(int i=1;i<n;i++) if((p[k].y>p[i].y)||(p[k].y==p[i].y&&p[k].x>p[i].x)) k=i;
    24     t=p[0],p[0]=p[k],p[k]=t;
    25     sort(p+1,p+n,cmp);
    26     s[0]=p[0],s[1]=p[1],s[2]=p[2];
    27     for(int i=3;i<n;i++){
    28         while(top&&mul(p[i],s[top],s[top-1])>=0) top--;
    29         s[++top]=p[i];
    30     }
    31     s[++top]=s[0];
    32     for(int i=0;i<top;i++) ans+=dis(s[i],s[i+1]);
    33 }
    34 int main(){
    35     freopen("fc.in","r",stdin);
    36     freopen("fc.out","w",stdout);
    37     scanf("%d",&n);
    38     for(int i=0;i<n;i++)
    39     scanf("%lf%lf",&p[i].x,&p[i].y);
    40     graham();
    41     printf("%.2lf",ans);
    42     return 0;
    43 }
  • 相关阅读:
    图片无缝横向滚动
    MySQL命令小结
    Git初级
    VS2012 创建的entityframework 4.1版本
    IE10 下系统出现Unable to get property 'PageRequestManager' of undefined or null reference错误
    MIME Types
    不兼容的数据类型
    使用Lambda .map函数将入参List转换至其它List
    MySQL中那种数据类型是只有true和false的
    ELK Stack
  • 原文地址:https://www.cnblogs.com/J-william/p/7341284.html
Copyright © 2011-2022 走看看