USACO Section 5.1 Fencing the Cows(凸包)

时间:2022-01-31 00:51:21

USACO Section 5.1 Fencing the Cows(凸包)

裸的凸包..很好写,废话不说,直接贴代码。

-----------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define rep(i,r) for(int i=0;i<r;i++)
using namespace std;
const int maxn=10000+5;
struct P {
    
    double x,y;
    
    P(double x=0,double y=0):x(x),y(y) {}
    
    P operator + (P o) { return P(x+o.x,y+o.y); }
    P operator - (P o) { return P(x-o.x,y-o.y); }
    
    
    bool operator == (const P &o) const {
        return x==o.x && y==o.y;
    }
    bool operator < (const P &o) const {
        return x<o.x || (x==o.x && y<o.y);
    }
    
};
P ans[maxn],p[maxn];
inline double cross(P a,P b) { return a.x*b.y-a.y*b.x; }
inline double dist(P o) { return sqrt(o.x*o.x+o.y*o.y); }
double convexHull(int n) {
    
    sort(p,p+n);
    
    int m=0;
    rep(i,n) {
        while(m>1 && cross(ans[m-1]-ans[m-2],p[i]-ans[m-2])<=0) m--;
        ans[m++]=p[i];
    }
    
    int k=m;
    for(int i=n-2;i>=0;i--) {
        while(m>k && cross(ans[m-1]-ans[m-2],p[i]-ans[m-2])<=0) m--;
        ans[m++]=p[i];
    }
    
    if(n>1) m--;
    
    double length=0;
    rep(i,m) length+=dist(ans[i]-ans[i+1]);
    
    return length;
    
}
    
int main()
{
    freopen("fc.in","r",stdin); freopen("fc.out","w",stdout);
    
    
    int n;
    cin>>n;
    rep(i,n) scanf("%lf%lf",&p[i].x,&p[i].y);
    printf("%.2lf\n",convexHull(n));
    
    
    return 0;
}

-----------------------------------------------------------------------------

Fencing the Cows
Hal Burch

Farmer John wishes to build a fence to contain his cows, but he's a bit short on cash right. Any fence he builds must contain all of the favorite grazing spots for his cows. Given the location of these spots, determine the length of the shortest fence which encloses them.

PROGRAM NAME: fc

INPUT FORMAT

The first line of the input file contains one integer, N. N (0 <= N <= 10,000) is the number of grazing spots that Farmer john wishes to enclose. The next N line consists of two real numbers, Xi and Yi, corresponding to the location of the grazing spots in the plane (-1,000,000 <= Xi,Yi <= 1,000,000). The numbers will be in decimal format.

SAMPLE INPUT (file fc.in)

4
4 8
4 12
5 9.3
7 8

OUTPUT FORMAT

The output should consists of one real number, the length of fence required. The output should be accurate to two decimal places.

SAMPLE OUTPUT (file fc.out)

12.00

USACO Section 5.1 Fencing the Cows(凸包)的更多相关文章

  1. 洛谷 P2742 &lbrack;USACO5&period;1&rsqb;圈奶牛Fencing the Cows &vert;&vert; 凸包模板

    整篇都是仅做记录... 蓝书上的板子.水平序,单调栈.先求下凸包,再求上凸包.叉积的作用是判定向量的位置关系. 48行的作用是在求上凸包的时候不至于去删下凸包中的点.上凸包中第一个点被认为是t1. 另 ...

  2. USACO 5&period;1 Fencing the Cows

    Fencing the CowsHal Burch Farmer John wishes to build a fence to contain his cows, but he's a bit sh ...

  3. poj3348 Cows 凸包&plus;多边形面积 水题

    /* poj3348 Cows 凸包+多边形面积 水题 floor向下取整,返回的是double */ #include<stdio.h> #include<math.h> # ...

  4. LG2742 【模板】二维凸包 &sol; &lbrack;USACO5&period;1&rsqb;圈奶牛Fencing the Cows

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

  5. luogu P2742 【模板】二维凸包 &sol; &lbrack;USACO5&period;1&rsqb;圈奶牛Fencing the Cows

    题解: 二维凸包裸题 按照x坐标为第一关键字,y坐标为第二关键字排序 然后相邻判断叉积用单调队列搞过去 正反都做一次就好了 代码: #include <bits/stdc++.h> usi ...

  6. &lbrack;洛谷P2742&rsqb;【模板】二维凸包&lpar;&lbrack;USACO5&period;1&rsqb;圈奶牛Fencing the Cows&rpar;

    题目大意:求一个点集凸包边长 题解:求凸包,直接求 卡点:发现在较后面数位上有较小的误差,还以为是浮点数误差,最后发现是构造函数写成了$int$类型 C++ Code: #include <al ...

  7. 【模板】二维凸包 &sol; &lbrack;USACO5&period;1&rsqb;圈奶牛Fencing the Cows

    Problem surface 戳我 Meaning 坐标系内有若干个点,问把这些点都圈起来的最小凸包周长. 这道题就是一道凸包的模板题啊,只要求出凸包后在计算就好了,给出几个注意点 记得检查是否有吧 ...

  8. P2742 【模板】二维凸包 &sol; &lbrack;USACO5&period;1&rsqb;圈奶牛Fencing the Cows

    题意:n个点,求凸包周长.(纯板子QAQ) 定义 凸包:用最小的凸多边形将n个点围在里面的图形为凸包 前置 向量:点积:(a,b) (c,d)=(a*c,b*d) =|(a,b)|*|(c,d)|*c ...

  9. USACO Section 1&period;2 Milking Cows 解题报告

    题目 题目描述 有3个农夫每天早上五点钟便起床去挤牛奶,现在第一个农夫挤牛奶的时刻为300(五点钟之后的第300个分钟开始),1000的时候结束.第二个农夫从700开始,1200结束.最后一个农夫从1 ...

随机推荐

  1. Linux&lpar;五&rpar;&lowbar;&lowbar;硬盘分区

    Linux中的文件管理机制是一种叫挂载和卸载的方式使用分区中的文件. 1.硬盘分区的概念 概述:首先我们要对硬盘分区的基本概念进行一些初步的了解,硬盘的分区主要分为基本分区(Primary Parti ...

  2. &lt&semi;&percnt;&num;Eval if判断用法

    1.绑定Repeater 基础用法 <%#Eval("RoleID")%> 2.简单判断用法 <td> <%# Convert.ToBoolean(E ...

  3. python课程第一周重点记录

  4. git使用--git命令项目提交问题总结

    提交遇到Error  "remote ref does not exist"解决办法:git fetch -p MY_REMOTE    eg.    git fetch -p o ...

  5. 优酷、YouTube、Twitter及JustinTV视频网站架构设计笔记

    本文是整理的关于优酷.YouTube.Twitter及JustinTV几个视频网站的架构或笔记,对于不管是视频网站.门户网站或者其它的网站,在架构上都有一定的参考意义,毕竟成功者的背后总有值得学习的地 ...

  6. Maven中聚合与继承

    何为继承? --›继承为了消除重复,我们把很多相同的配置提取出来 --›例如:grouptId,version等 就像写java程序一样,对于有共性切重复的东西,就提取出来. 如有三个pom.xml配 ...

  7. 【转载】svn代码回滚命令

    [说明]转载自 http://www.cnblogs.com/jndream/archive/2012/03/20/2407955.html 取消对代码的修改分为两种情况:   第一种情况:改动没有被 ...

  8. Android安全bug ANDROID-8219321

    ANDROID-8219321漏洞主要源自Android ZipFile函数漏洞:没有进行校验重名entry逻辑漏洞,逻辑漏洞细节详见Google+文章和Bluebox Security提报Andro ...

  9. 迭代导出word 文档

    Map迭代的使用: Map map = new HashMap() ; Iterator it = map.entrySet().iterator() ; while (it.hasNext()) { ...

  10. PDF文件怎么修改,PDF文件编辑方法

    PDF文件是一种独特的文件,在日常办公中已经成为我们使用最广泛的电子文档格式.在使用PDF文件中会遇到PDF文件有错区的时候,再从新制作一个PDF文件会比较麻烦,只能通过工具来对PDF文件进行修改,这 ...