Fencing the Cows_usaco 5.1_凸包
2016-12-01 19:53
555 查看
Description
农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏。他建造的围栏必须包括他的奶牛喜欢吃草的所有地点。对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度。Input
输入数据的第一行包括一个整数 N。N(0 <= N <= 10,000)表示农夫约翰想要围住的放牧点的数目。接下来 N 行,每行由两个实数组成,Xi 和 Yi,对应平面上的放牧点坐标(-1,000,000 <= Xi,Yi <= 1,000,000)。数字用小数表示。Output
输出必须包括一个实数,表示必须的围栏的长度。答案保留两位小数。Analysis
裸的凸包,这么良心的题目已经很少见了Code
/* ID:wjp13241 PROG:fc LANG:C++ */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> #include <math.h> #include <vector> #include <stack> #include <queue> #define fo(i,a,b) for (int i=a;i<=b;i++) #define dfo(i,a,b) for (int i=a;i>=b;i--) #define fil(x,t) memset(x,t,sizeof(x)) #define min(x,y) x<y?x:y #define max(x,y) x>y?x:y #define ll long long #define INF 0x7f7f7f7f #define EPS 1e-4 #define L 351 #define N 10001 #define E N*N+1 using namespace std; struct pos{ double x,y; bool operator<(pos a){return a.x<x||a.x==x&&a.y<y;} }t ; int s ; double cros(pos a,pos b,pos c){return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);} double dist(pos a,pos b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));} bool cmp(pos a,pos b){return ((cros(a,b,t[0])>0)||(!(cros(a,b,t[0]))&&(dist(a,t[0])<dist(b,t[0]))));} int main() { freopen("fc.in","r",stdin); freopen("fc.out","w",stdout); ios::sync_with_stdio(false); int n; scanf("%d",&n); { for (int i=0;i<n;i++) { scanf("%lf%lf",&t[i].x,&t[i].y); if (t[i]<t[0]) { pos tmp=t[i]; t[i]=t[0]; t[0]=tmp; } } sort(t+1,t+n,cmp); int top=3; s[1]=0; s[2]=1; s[3]=2; for (int i=3;i<n;i++) { while (cros(t[i],t[s[top]],t[s[top-1]])>=0) --top; s[++top]=i; } double ans=dist(t[s[1]],t[s[top]]); if (n==1) ans=0; else if (n==2) ans=dist(t[s[1]],t[s[2]])*2; else for (int i=1;i<top;i++) ans+=dist(t[s[i]],t[s[i+1]]); printf("%.2lf\n",ans); } return 0; }
相关文章推荐
- USACO Section 5.1 Fencing the Cows - 凸包模板题~~
- 凸包——Luogu2742 [USACO5.1]圈奶牛Fencing the Cows
- [USACO5.1]圈奶牛Fencing the Cows(凸包模板)
- USACO 5.1 Fencing the Cows(凸包)
- USACO Section 5.1 Fencing the Cows(凸包)
- usaco 5.1 Fencing the Cows(凸包模板题)
- 洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows
- P2742 [USACO5.1]圈奶牛Fencing the Cows
- [USACO5.1]Fencing the Cows
- USACO 5.1 Fencing the Cows
- 太感动了!第一次自己打出了USACO的题~ [USACO5.1]圈奶牛Fencing the Cows
- [luogu2742]:[USACO5.1]圈奶牛Fencing the Cows
- P2742 [USACO5.1]圈奶牛Fencing the Cows
- USACO 5.1 fencing the cows——计算几何/凸包模板
- USACO Fencing the Cows 解题报告
- USACO Section 5.1 Fencing the Cows
- Fencing the Cows [USACO]
- usaco fencing the cows
- 【USACO 5.1.1】Fencing the Cows
- usaco 5.1.1 Fencing the Cows