描述

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

PROGRAM NAME: fc

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凸包算法
注意读进来的有小数
 /*by SilverN*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int mxn=;
struct node{
double x,y;
}p[mxn],s[mxn];
int top;
double ans;
int n;
inline node operator -(node a,node b){
node T; T.x=a.x-b.x; T.y=a.y-b.y; return T;
}
inline double operator *(node a,node b){
return a.x*b.y-a.y*b.x;
}
inline double dis(node a,node b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
inline bool operator < (node a,node b){
ll t=(a-p[])*(b-p[]);
if(t==)return dis(a,p[])<dis(b,p[]);
return t>;
}
void graham(){
int i,j;
int t=;
for(i=;i<=n;i++){
if(p[i].y<p[t].y || (p[i].y==p[t].y && p[i].x<p[t].x))t=i;
}
swap(p[],p[t]);
sort(p+,p+n+);
s[]=p[];
s[]=p[];top=;
for(i=;i<=n;i++){
while(top && ((s[top]-s[top-])*(p[i]-s[top-]))< )top--;
s[++top]=p[i];
}
s[top+]=p[];
for(i=;i<=top;i++){
ans+=sqrt(dis(s[i],s[i+]));
}
return;
}
int main(){
// freopen("fc.in","r",stdin);
// freopen("fc.out","w",stdout);
scanf("%d",&n);
int i,j;
for(i=;i<=n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
graham();
printf("%.2lf\n",ans);
return ;
}

Andrew凸包

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
struct point{
double x,y;
point operator + (const point &b){return (point){x+b.x,y+b.y};}
point operator - (const point &b){return (point){x-b.x,y-b.y};}
point operator * (const double v){return (point){x*v,y*v};}
bool operator < (const point &b)const{
return x<b.x || (x==b.x && y<b.y);
}
}a[mxn];
double dot(const point &a,const point &b){return a.x*b.x,a.y*b.y;}
double Cross(const point &a,const point &b){return a.x*b.y-a.y*b.x;}
double dist(const point &a,const point &b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int n;
int st[mxn],top,t2;
void andrew(){
a[n+]=a[];
top=t2=;
for(int i=;i<=n;i++){
while(top> && Cross(a[i]-a[st[top-]],a[st[top]]-a[st[top-]])>=)top--;
st[++top]=i;
}
t2=top;
for(int i=n-;i;i--){
while(t2>top && Cross(a[i]-a[st[t2-]],a[st[t2]]-a[st[t2-]])>=)t2--;
st[++t2]=i;
}
return;
}
int main(){
// freopen("fc.in","r",stdin);
// freopen("fc.out","w",stdout);
int i,j;
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
sort(a+,a+n+);
andrew();
// for(i=1;i<=t2;i++)
// printf("%.3f %.3f\n",a[st[i]].x,a[st[i]].y);
double ans=;
for(i=;i<t2;i++)
ans+=dist(a[st[i]],a[st[i+]]);
printf("%.2f\n",ans);
return ;
}

cogs896 圈奶牛的更多相关文章

  1. 【COGS & USACO】896. 圈奶牛(凸包)

    http://cojs.tk/cogs/problem/problem.php?pid=896 我的计算几何入门题... 看了看白书的计算几何部分,,恩好嘛.. 乃们都用向量!!!! 干嘛非要将2个点 ...

  2. LG2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows

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

  3. cogs 896. 圈奶牛

    ★★☆   输入文件:fc.in   输出文件:fc.out   简单对比 时间限制:1 s   内存限制:128 MB 描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必 ...

  4. 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows

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

  5. 洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows

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

  6. P2742 [USACO5.1]圈奶牛Fencing the Cows

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

  7. luogu P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows

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

  8. [洛谷P2742]【模板】二维凸包([USACO5.1]圈奶牛Fencing the Cows)

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

  9. P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows

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

随机推荐

  1. 查​看​和​设​置​o​r​a​c​l​e​数​据​库​的​最​大​连​接​数

    当前的连接数 select count(*) from v$process; 设置的最大连接数(默认值为150) select value from v$parameter where name = ...

  2. smarty缓存控制

    第一步初始化配置文件中设置 如果当前访问的模板有缓存就不需要连接数据库那些代码了,如果要模板局部不缓存,要写在iscache外,模板中用{nocache}

  3. 或得的一个div是变量时

  4. 15Mybatis_输出类型

    输出类型分为两种:1.resultType          和         2.resultMap 接下来先讲解resultType: 使用resultType进行输出映射,只有查询出来的列名和 ...

  5. SQL 常见函数使用

    1.字符串转化为整型 CONVERT(INT,'字符串') 2.结果集 输出为一段字符串 SELECT STUFF((SELECT ','+A FROM tableFOR XML PATH('')), ...

  6. [转]2006 MySQL server has gone away错误,最大值溢出解决办法 mysql max_allowed_packet 查询和修改

    From : http://www.cnblogs.com/huangcong/archive/2013/03/26/2981790.html 1.应用程序(比如PHP)长时间的执行批量的MYSQL语 ...

  7. 在opencv3中利用SVM进行图像目标检测和分类

    采用鼠标事件,手动选择样本点,包括目标样本和背景样本.组成训练数据进行训练 1.主函数 #include "stdafx.h" #include "opencv2/ope ...

  8. python多进程共享变量Value使用tips

    前言: 在使用tornado的多进程时,需要多个进程共享一个状态变量,于是考虑使用multiprocessing.Value(对于该变量的具体细节请查阅相关资料).在根据网上资料使用Value时,由于 ...

  9. 关于matlab中特殊字符, 上标和下标

    'T=25\circC',(摄氏度) 下标用 _{下划线} 上标用^ (尖号) 希腊字母等特殊字符用 α \alpha β \beta γ \gamma θ \theta Θ \Theta Г \Ga ...

  10. Win7 Qt4.8.5+QtCreator2.8.0+mingw配置过程

    1:安装包 百度盘下载链接: Mingw: :安装步骤 1.首先安装qt creator,双击qt-creator-windows-opensource-2.8.0,注意安装目录不要有空格和特殊字符, ...