P2742 [USACO5.1]圈奶牛Fencing the Cows
来源:互联网 发布:淘宝直播可以用电脑吗 编辑:程序博客网 时间:2024/05/02 03:11
题目描述
农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏。他建造的围栏必须包括他的奶牛喜欢吃草的所有地点。对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度。
输入输出格式
输入格式:输入数据的第一行包括一个整数 N。N(0 <= N <= 10,000)表示农夫约翰想要围住的放牧点的数目。接下来 N 行,每行由两个实数组成,Xi 和 Yi,对应平面上的放牧点坐标(-1,000,000 <= Xi,Yi <= 1,000,000)。数字用小数表示。
输出格式:输出必须包括一个实数,表示必须的围栏的长度。答案保留两位小数。
输入输出样例
44 84 125 9.37 8
12.00
说明
题目翻译来自NOCOW。
USACO Training Section 5.1
题解:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=10000+5;
struct node{
double x,y;
}a[maxn];
int stack[maxn],top;
bool cmp(node p,node q){return p.x<q.x;}
double dist(int u,int v){
return sqrt((a[u].x-a[v].x)*(a[u].x-a[v].x)+(a[u].y-a[v].y)*(a[u].y-a[v].y));
}
bool pd(int u,int v,int w){
return (a[u].y-a[v].y)*(a[v].x-a[w].x)>(a[v].y-a[w].y)*(a[u].x-a[v].x);
}
int main(){
int i,j,m,n,k;
cin>>n;
for(i=1;i<=n;i++)cin>>a[i].x>>a[i].y;
sort(a+1,a+n+1,cmp);
top=2;
stack[1]=1;stack[2]=2;
for(i=3;i<=n;i++){
while(top>1 && !pd(i,stack[top],stack[top-1])) top--;
stack[++top]=i;
}
double ans=0;
for(i=1;i<top;i++)ans+=dist(stack[i],stack[i+1]);
top=2;
stack[1]=1;stack[2]=2;
for(i=3;i<=n;i++){
while(top>1 && pd(i,stack[top],stack[top-1] )) top--;
stack[++top]=i;
}
for(i=1;i<top;i++)ans+=dist(stack[i],stack[i+1]);
printf("%.2f\n",ans);
return 0;
}
- P2742 [USACO5.1]圈奶牛Fencing the Cows
- [luogu2742]:[USACO5.1]圈奶牛Fencing the Cows
- 凸包——Luogu2742 [USACO5.1]圈奶牛Fencing the Cows
- [USACO5.1]圈奶牛Fencing the Cows(凸包模板)
- 太感动了!第一次自己打出了USACO的题~ [USACO5.1]圈奶牛Fencing the Cows
- [USACO5.1]Fencing the Cows
- JZOJ1312.【USACO题库】5.1.1 Fencing the Cows圈奶牛
- usaco5.1.1 Fencing the Cows
- 计算几何 usaco Fencing the Cows 圈奶牛
- USACO 5.1.1 Fencing the Cows 圈奶牛 题解与分析
- USACO5.1.1 Fencing the Cows (fc)
- USACO5.1.1 Fencing the Cows(fc)
- [USACO5.1.1] 圈奶牛
- 【USACO5.1.1】圈奶牛(BSOI2142)
- usaco 5.1.1 Fencing the Cows
- Fencing the Cows
- usaco fencing the cows
- Section 5.1 Fencing the Cows
- 创业公司的个人“可伸缩性”方案
- java Runtime类
- Hadoop系统框架安全机制(2017-01-17)
- 蓝牙简介
- elastic-job开源框架使用中遇到的 架包冲突错误
- P2742 [USACO5.1]圈奶牛Fencing the Cows
- tornado+gevent
- 解决报错:error: The requested URL returned error: 401 Unauthorized while accessing
- iOS蓝牙开发 - GameKit框架
- [HAOI2007]理想正方形-单调队列学习笔记
- pat 1088
- caffe faster-rcnn make -j8 报错 ,CentOS
- 简易:ListView滑动隐藏标题栏
- iO蓝牙开发 - BLE(蓝牙4.0)