描述

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

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. jstat使用

    jstat -gcutil pid 统计gc信息统计.其中最后五项,分别是young gc的次数,young gc的时间,full gc的次数,full gc的时间,gc的总时间.

  2. iOS学习资料

    1. UI整理 http://www.cocoachina.com/ios/20151110/14067.html. 2. iOS学习路径 http://www.cocoachina.com/ios/ ...

  3. javascript中的表结构

    列表是一种常见的数据结构,通常列表是一族有徐的数据,列表中的数据项称为元素.在javascript中列表中的数据可以是任意类型的,列表中可以保存多少元素没有事先限定,实际使用时元素的数量只收到程序内内 ...

  4. 如何在mac上安装gradle

    首先,先download最新版本的gradle,网址如下:http://www.gradle.org/get-started然后将下载下来的zip包放在你要安装的路径上,我安装在/usr/local/ ...

  5. homepage左边的导航菜单怎么做的?

    homepage左边的导航菜单怎么做的? 为啥只在homepage页面写了一个div 然后用一个homepage.js来填充这个div  然后用一个外部容器ID作为homepage.js的参数

  6. [6]Telerik TreeView 复选框

    参考连接:http://demos.telerik.com/aspnet-mvc/razor/treeview/clientsideapi 问题: Telerik TreeView 选择或取消 父选项 ...

  7. cisco交换技术list

  8. 【Android性能优化】(一)使用SparseIntArray替换HashMap

    SparseArray是android里为<Interger,Object>这样的Hashmap而专门写的class,目的是提高效率,其核心是折半查找函数(binarySearch)

  9. C语言 百炼成钢8

    //题目22:两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人.已抽签决定 //比赛名单.有人向队员打听比赛的名单.a说他不和x比,c说他不和x, z比,请编程序找出 //三 ...

  10. [转]World Wind Java开发之四——搭建本地WMS服务器

    在提供地理信息系统客户端时,NASA还为用户提供了开源的WMS Server 服务器应用:World Wind WMS Server.利用这个应用,我们可以架设自己的WMS服务并使用自己的数据(也支持 ...