USACO 5.1 Fencing the Cows(凸包)

注意传参的 类型。。。模版啊。。。

 1 /*

 2 ID:cuizhe

 3 LANG: C++

 4 TASK: fc

 5 */

 6 #include <cstdio>

 7 #include <cstring>

 8 #include <cmath>

 9 #include <queue>

10 #include <iostream>

11 #include <algorithm>

12 using namespace std;

13 #define eps 1e-6

14 struct Point

15 {

16     double x,y;

17 }p[10001];

18 int s[10001];

19 double det(double x1,double y1,double x2,double y2)

20 {

21     return x1*y2 - x2*y1;

22 }

23 double cross(Point a,Point b,Point c)

24 {

25     return det(a.x-c.x,a.y-c.y,b.x-c.x,b.y-c.y);

26 }

27 double dis(Point a,Point b)

28 {

29     return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)+eps);

30 }

31 bool cmp(Point a,Point b)

32 {

33     double t;

34     t = cross(a,b,p[0]);

35     if(t > 0||(t == 0&&dis(a,p[0]) < dis(b,p[0])))

36        return true;

37     else

38        return false;

39 }

40 int main()

41 {

42     int n,i,top;

43     Point temp;

44     double ans;

45     freopen("fc.in","r",stdin);

46     freopen("fc.out","w",stdout);

47     scanf("%d",&n);

48     for(i = 0;i < n;i ++)

49     {

50         scanf("%lf%lf",&p[i].x,&p[i].y);

51         if((p[i].y < p[0].y)||(p[i].y == p[0].y&&p[i].x < p[0].x))

52         {

53             temp = p[i];

54             p[i] = p[0];

55             p[0] = temp;

56         }

57     }

58     sort(p+1,p+n,cmp);

59     for(i = 0;i < 3;i ++)

60     {

61         s[i] = i;

62     }

63     top = 2;

64     for(i = 3;i < n;i ++)

65     {

66         while(top >= 1&&cross(p[i],p[s[top]],p[s[top-1]]) >= 0)

67         top --;

68         s[++top] = i;

69     }

70     s[top+1] = 0;

71     ans = 0;

72     for(i = 0;i <= top;i ++)

73     {

74         ans += dis(p[s[i]],p[s[i+1]]);

75     }

76     printf("%.2f\n",ans+eps);

77     return 0;

78 }

 

你可能感兴趣的:(USACO)