USACO 5.1 Fencing the Cows
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
————————————————————————————————题解
一道凸包求周长的题,卷包裹算法,如果栈中的后两个点组成的向量和新加入的点和最后点的向量是顺时针旋转,那么就不符合凸包的性质,弹出直到符合性质即可
/*
ID: ivorysi
LANG: C++
TASK: fc
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#include <cmath>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x7fffffff
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
struct vec {
double x,y;
vec(double x=0.0,double y=0.0) :x(x),y(y) {}
vec operator - (const vec &b) const{
return vec(x-b.x,y-b.y);
}
double norm() const{
return x*x+y*y;
}
}a[];
int n;
bool dcmp(double a,double b=0.0) {
return fabs(a-b) < esp ? : ;
}
double cross(vec a,vec b) {
return a.x*b.y-a.y*b.x;
}
inline bool cmp(const vec &c,const vec &d) {
vec t1= c-a[],t2=d-a[];
double t=cross(t1,t2);
if(!dcmp(t)) return t>;//逆时针为正,顺时针为负
else return c.norm()<d.norm();
}
double o(double a){
return a*a;
}
struct poly {
vector<vec> poi;
double peri() {
double res=0.0;
siji(i,,poi.size()-) {
res+=sqrt(o(poi[i].x-poi[i-].x)+o(poi[i].y-poi[i-].y));
}
res+=sqrt(o(poi[].x-poi[poi.size()-].x)+o(poi[].y-poi[poi.size()-].y));
return res;
}
void convex() {
int id=;
siji(i,,n) {
if(a[i].x<a[id].x || (a[i].x==a[id].x && a[i].y<a[id].y)) {
id=i;
}
}
if(id!=) swap(a[id],a[]);
sort(a+,a++n,&cmp);
poi.push_back(a[]);
siji(i,,n) {
while(poi.size() >= &&
cross(poi[poi.size()-]-poi[poi.size()-],a[i]-poi[poi.size()-])<=)
poi.pop_back();
poi.push_back(a[i]);
}
}
}tw; void solve() {
scanf("%d",&n);
siji(i,,n) {
scanf("%lf%lf",&a[i].x,&a[i].y);
}
tw.convex();
printf("%.2lf\n",tw.peri());
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("fc.in","r",stdin);
freopen("fc.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
USACO 5.1 Fencing the Cows的更多相关文章
- USACO Section 5.1 Fencing the Cows(凸包)
裸的凸包..很好写,废话不说,直接贴代码. ----------------------------------------------------------------------------- ...
- [BZOJ 1652][USACO 06FEB]Treats for the Cows 题解(区间DP)
[BZOJ 1652][USACO 06FEB]Treats for the Cows Description FJ has purchased N (1 <= N <= 2000) yu ...
- NC24017 [USACO 2016 Jan S]Angry Cows
NC24017 [USACO 2016 Jan S]Angry Cows 题目 题目描述 Bessie the cow has designed what she thinks will be the ...
- LG2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
题意 题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 ...
- 洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows
题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 输入 ...
- P2742 [USACO5.1]圈奶牛Fencing the Cows
题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 输入 ...
- USACO Section 1.2 Milking Cows 解题报告
题目 题目描述 有3个农夫每天早上五点钟便起床去挤牛奶,现在第一个农夫挤牛奶的时刻为300(五点钟之后的第300个分钟开始),1000的时候结束.第二个农夫从700开始,1200结束.最后一个农夫从1 ...
- luogu P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
题解: 二维凸包裸题 按照x坐标为第一关键字,y坐标为第二关键字排序 然后相邻判断叉积用单调队列搞过去 正反都做一次就好了 代码: #include <bits/stdc++.h> usi ...
- [洛谷P2742]【模板】二维凸包([USACO5.1]圈奶牛Fencing the Cows)
题目大意:求一个点集凸包边长 题解:求凸包,直接求 卡点:发现在较后面数位上有较小的误差,还以为是浮点数误差,最后发现是构造函数写成了$int$类型 C++ Code: #include <al ...
随机推荐
- jquery.lazyload插件实现图片延迟加载详解
什么是LazyLoad技术? 在页面上图片比较多的时候,打开一张页面必然引起与服务器大数据量的交互.尤其是对于高清晰的图片,占了几百K的空间.Lazy Load 是一个用 JavaScript 编写的 ...
- LocalDateTime与字符串互转/Date互转/LocalDate互转/指定日期/时间比较
Java 8中表示日期和时间的类有多个,主要的有: Instant:表示时刻,不直接对应年月日信息,需要通过时区转换 LocalDateTime: 表示与时区无关的日期和时间信息,不直接对应时刻,需要 ...
- NCPC2016-E- Exponial
题目描述 Illustration of exponial(3) (not to scale), Picture by C.M. de Talleyrand-Périgord via Wikimedi ...
- pta 一
7-1 打印沙漏 (20 分) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数 ...
- HDU 1717 小数化分数2 数学题
解题报告:输入一个小于1的小数,让你把这个数转化成分数,但注意,输入的数据还有无限循环的小数,循环节用一对括号包含起来. 之前还没有写过小数转分数的题,当然如果没有循环小数的话,应该比较简单,但是这题 ...
- 爬虫笔记之JS检测浏览器开发者工具是否打开
在某些情况下我们需要检测当前用户是否打开了浏览器开发者工具,比如前端爬虫检测,如果检测到用户打开了控制台就认为是潜在的爬虫用户,再通过其它策略对其进行处理.本篇文章主要讲述几种前端JS检测开发者工具是 ...
- 《区块链100问》第73集:达世币Dash是什么?
达世币诞生于2014年1月18日,匿名程度较比特币更高. 达世币有三种转账方式,一是像比特币一样的普通转账:二是即时交易.不需要矿工打包确认,就可以确认交易,几乎可以实现秒到:三是匿名交易.从区块链上 ...
- 移动开发关于APN的知识整理
APN(Access Point Name),即"接入点名称",用来标识GPRS的业务种类,是通过手机上网时必须配置的一个参数,其决定了手机通过哪种接入方式来访问网络. 一.类别 ...
- 原生的js实现jsonp的跨域封装
一.原理 jsonp是利用浏览器请求script文件时不受同源策略的限制而实现的,伪造一个script标签,将请求数据的url赋值给script的src属性,并将该标签添加到html中,浏览器会自动发 ...
- Linux学习笔记-Linux系统简介
Linux学习笔记-Linux系统简介 UNIX与Linux发展史 UNIX是父亲,Linux是儿子. UNIX发行版本 操作系统 公司 硬件平台 AIX IBM PowerPC HP-UX HP P ...