cogs 896. 圈奶牛

                ★★☆   输入文件:fc.in   输出文件:fc.out   简单对比
                  时间限制:1 s   内存限制:128 MB

描述

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

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

题解:裸凸包求长度,上模板。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<vector>
 9 using namespace std;
10 const int eps=1e-7;
11 int N,top;
12 double ans;
13 struct P{
14     double x,y;
15     friend P operator-(P a,P b){
16         P t; t.x=a.x-b.x; t.y=a.y-b.y;
17         return t;
18     }
19     friend double operator*(P a,P b){
20         return ((a.x*b.y)-(b.x*a.y));
21     }
22     friend double dis(P a,P b){
23         return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
24     }
25 }p[20000],stac[20000];
26 inline bool operator<(P a,P b){
27     double t=(a-p[1])*(b-p[1]);
28     if(fabs(t)<=eps) return dis(a,p[1])<dis(b,p[1]);
29     return t>eps;
30 }
31 inline void graham(){
32     int tmp=1;
33     for(int i=2;i<=N;i++){
34         if(p[i].y<p[tmp].y||(fabs(p[i].y-p[tmp].y)<=eps&&p[i].x<p[tmp].x)) tmp=i;
35     }
36     swap(p[1],p[tmp]);
37     sort(p+2,p+N+1);
38     stac[++top]=p[1]; stac[++top]=p[2];
39     for(int i=3;i<=N;i++){
40         while((stac[top]-stac[top-1])*(p[i]-stac[top-1])<=eps) top--;
41         stac[++top]=p[i];
42     }
43     stac[top+1]=p[1];
44     for(int i=1;i<=top;i++){
45         ans+=dis(stac[i],stac[i+1]);
46     }
47     printf("%.2lf",ans);
48 }
49 int main(){
50     freopen("fc.in","r",stdin);
51     freopen("fc.out","w",stdout);
52     scanf("%d",&N);
53     for(int i=1;i<=N;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
54     graham();
55     return 0;
56 } 
原文地址:https://www.cnblogs.com/CXCXCXC/p/5244262.html