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 ...
随机推荐
- 使用clone( )和Cloneable接口
由Object类定义的绝大部分方法在本书其他部分讨论.而一个特别值得关注的方法是clone( ).clone( )方法创建调用它的对象的一个复制副本.只有那些实现Cloneable接口的类能被复制. ...
- Idea maven tomcat 配置热更新 以及 maven jar依赖
看了视频 实在忍不住上了idea的贼船 不过这玩意确实有点坑爹,因为用的人少,所以很多配置是有问题的 例如maven配置tomcat热更新 以及tomcat的maven配置 我这里放几张图作为备用 配 ...
- 让input框只能输入数字
var oInput = document.querySelector("input");oInput.onkeyup = function () { var value = th ...
- 实战:ADFS3.0单点登录系列-总览
本系列将以一个实际项目为背景,介绍如何使用ADFS3.0实现SSO.其中包括SharePoint,MVC,Exchange等应用程序的SSO集成. 整个系列将会由如下几个部分构成: 实战:ADFS3. ...
- es2自动发现插件
https://www.elastic.co/guide/en/elasticsearch/plugins/current/discovery-multicast-usage.html http:// ...
- 使用C#改变鼠标的指针形状
1.在一个无标题的窗体中用MOUSEMOVE事件判断鼠标坐标是否到达窗体的边缘,如果是的话将鼠标指针改为可调整窗体大小的双向箭头. private void Form1_MouseMove(o ...
- ScrollView 简单出错
ScrollView can host only one direct child 主要是ScrollView内部只能有一个子元素,即不能并列两个子元素,所以需要把所有的子元素放到一个LinearLa ...
- 用css画出对话框
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa4AAAFSCAYAAACqpTv4AAAgAElEQVR4nO3deZBU5b3GcUIlVTG3bi
- http长链接与短链接
http://www.cnblogs.com/cswuyg/p/3653263.html keep-live模式 这个博客写的很全:http://www.cnblogs.com/skynet/arch ...
- The entity type <type> is not part of the model for the current context
这是在网站里遇到的一个错误,自动生成的不能手动添加, reference: http://stackoverflow.com/questions/19695545/the-entity-type-xx ...