hdu 2202(凸包+枚举凸包上的点)

 1 /*

 2 *  题目要求:求三角形的最大面积 

 3 *  方法:凸包+枚举凸包上的点 

 4 */

 5 

 6 #include <cmath>

 7 #include <cstdio>

 8 #include <cstdlib>

 9 #include <iostream>

10 

11 using namespace std;

12 

13 const int N = 50005;

14 const double eps = 1e-8;

15 

16 struct point {

17     int x;

18     int y;

19 }p[N], stack[N];

20 

21 bool isZero(double x) {

22     return (x > 0 ? x : -x) < eps; 

23 }

24 

25 double dis(point A, point B) {

26     return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));

27 }

28 

29 int crossProd(point A, point B, point C) {

30     return (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x);

31 }

32 

33 int cmp(const void *a, const void *b) {

34     point *c = (point *)a;

35     point *d = (point *)b;

36     double k = crossProd(p[0], *c, *d);

37     if (k<eps || (isZero(k)&&dis(p[0], *c)>dis(p[0], *d))) return 1;

38     return -1;

39 }

40 

41 int Graham(int n) {

42     int x = p[0].x;

43     int y = p[0].y;

44     int mi = 0;

45     for (int i=1; i<n; ++i) {

46         if (p[i].x<x || (p[i].x==x&&p[i].y<y)) {

47             x = p[i].x;

48             y = p[i].y;

49             mi = i;

50         }

51     }

52     point tmp = p[mi];

53     p[mi] = p[0];

54     p[0] = tmp;

55     qsort(p+1, n-1, sizeof(point), cmp);

56     p[n] = p[0];

57     for (int i=0; i<3; ++i) stack[i] = p[i];

58     int top = 2;

59     for (int i=3; i<n; ++i) {

60         while (crossProd(stack[top-1], stack[top], p[i])<=eps && top>=2) --top;

61         stack[++top] = p[i];

62     }

63     return top;    

64 }

65 

66 double maxTrangle(int n) {

67     int top = Graham(n);

68     double area, maxArea = 0;

69     for (int i=0; i<top; ++i) {

70         for (int j=i+1; j<top; ++j) {

71             for (int k=j+1; k<=top; ++k) {

72                 area = crossProd(stack[i], stack[j], stack[k]);

73                 if (maxArea < area) maxArea = area; 

74             }

75         }

76     }

77     return maxArea * 0.5;

78 }

79 

80 int main() {

81     int n;

82     while (scanf("%d", &n) != EOF) {

83         for (int i=0; i<n; ++i) scanf ("%d%d", &p[i].x, &p[i].y);

84         double ans = maxTrangle(n);

85         printf ("%.2lf\n", ans);

86     }

87     return 0;

88 }

 

你可能感兴趣的:(HDU)