cogs896 圈奶牛
描述
农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏。他建造的围栏必须包括他的奶牛喜欢吃草的所有地点。对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度。
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 圈奶牛的更多相关文章
- 【COGS & USACO】896. 圈奶牛(凸包)
http://cojs.tk/cogs/problem/problem.php?pid=896 我的计算几何入门题... 看了看白书的计算几何部分,,恩好嘛.. 乃们都用向量!!!! 干嘛非要将2个点 ...
- LG2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
题意 题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 ...
- cogs 896. 圈奶牛
★★☆ 输入文件:fc.in 输出文件:fc.out 简单对比 时间限制:1 s 内存限制:128 MB 描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必 ...
- 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
Problem surface 戳我 Meaning 坐标系内有若干个点,问把这些点都圈起来的最小凸包周长. 这道题就是一道凸包的模板题啊,只要求出凸包后在计算就好了,给出几个注意点 记得检查是否有吧 ...
- 洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows
题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 输入 ...
- P2742 [USACO5.1]圈奶牛Fencing the Cows
题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 输入 ...
- 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 ...
- P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
题意:n个点,求凸包周长.(纯板子QAQ) 定义 凸包:用最小的凸多边形将n个点围在里面的图形为凸包 前置 向量:点积:(a,b) (c,d)=(a*c,b*d) =|(a,b)|*|(c,d)|*c ...
随机推荐
- Watir、Selenium2、QTP区别
1.支持的语言 Watir:ruby Selenium2:支持多种语言,如:python,ruby,java,c#,php,perl,javascript QTP:vbscript 2.支持的浏览器 ...
- sqlzoo.net刷题4
SELECT name, continent FROM world a WHERE population > ( FROM world b WHERE a.continent = b.conti ...
- 判断一个值是否在数组里,可以检测数字,字符串,json对象
Array.prototype.indexOf = function (val) {//判断数组是否存在某个值,如果存在返回该值对应的索引,否则返回-1 for (var i = 0; i < ...
- Microsoft Visual Studio 下载转帖
1.VS2010 2.VS2012 Visual Studio 2012 Ultimate旗舰版序列号: YKCW6-BPFPF-BT8C9-7DCTH-QXGWC YQ7PR-QTHDM-HCBCV ...
- 一个DOM元素绑定多个事件时,先执行冒泡还是捕获
绑定在被点击元素的事件是按照代码顺序发生,其他元素通过冒泡或者捕获“感知”的事件,按照W3C的标准,先发生捕获事件,后发生冒泡事件.所有事件的顺序是:其他元素捕获阶段事件 -> 本元素代码顺序事 ...
- [转]Hive/Beeline 使用笔记
FROM : http://www.7mdm.com/1407.html Hive: 利用squirrel-sql 连接hive add driver -> name&example u ...
- C语言 百炼成钢4
//题目10:打印楼梯,同时在楼梯上方打印两个笑脸. #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdli ...
- 加密算法使用(五):RSA使用全过程
RSA是一种非对称加密算法,适应RSA前先生成一对公钥和私钥. 使用公钥加密的数据可以用私钥解密,同样私钥加密的数据也可以用公钥解密, 不同之处在于,私钥加密数据的同事还可以生成一组签名,签名是用来验 ...
- EBS中使用java进行 JavaConcurrentProgram 请求获取参数
public class MainTest implements JavaConcurrentProgram { //实现interface中的runProgram方法 public void run ...
- “插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题
“插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题 最近做一个前端的项目,项目需要引用VLC浏览器插件,javascript在IE.Firefox等浏览器 ...