POJ 2079 求最大三角形面积 (凸包+旋转卡壳)

题目链接:http://poj.org/problem?id=2079


方法:凸包+旋转卡壳(O(n^2))

  1. #include<cstdio>  
  2. #include<cmath>  
  3. #include<algorithm>  
  4. #define N 50000  
  5. using namespace std;  
  6. //定义点  
  7. struct Point  
  8. {  
  9.     double x, y;  
  10.     Point(double x = 0, double y = 0) : x(x), y(y) {}  
  11. };  
  12. typedef Point Vector;  
  13. Point p[N+10];  
  14. Point ch[N+10];  
  15. const double eps = 1e-10;  
  16. int dcmp(double x)  
  17. {  
  18.     if(fabs(x) < eps) return 0;  
  19.     else return x < 0 ? -1 : 1;  
  20. }  
  21.   
  22. //点-点=向量  
  23. Vector operator - (Point A, Point B) {return Vector(A.x-B.x, A.y-B.y);}  
  24. double Cross(Vector A, Vector B){return A.x*B.y - A.y*B.x;}  
  25. bool cmp ( Point a, Point b )  
  26. {  
  27.     if ( a.x != b.x ) return a.x < b.x;  
  28.     else return a.y < b.y;  
  29. }  
  30. int ConvexHull(Point ch[], int n, Point p[])  
  31. {  
  32.     sort(p, p+n, cmp);  
  33.     int m = 0;  
  34.   
  35.     //下凸边  
  36.     for(int i = 0; i < n; i++)  
  37.     {  
  38.         while(m > 1 && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) m--;  
  39.         ch[m++] = p[i];  
  40.     }  
  41.   
  42.     int k = m;  
  43.     //上凸边  
  44.     for(int i = n-2; i >= 0; i--)  
  45.     {  
  46.         while(m > k && dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])) <= 0) m--;  
  47.         ch[m++] = p[i];  
  48.     }  
  49.   
  50.     if(n > 1)   m--;  
  51.     return m;  
  52.   
  53. }  
  54.   
  55. double rotaing_calipers(Point ch[], int n)  
  56. {  
  57.     int p;  
  58.     int i, j;  
  59.     double ans = 0;  
  60.     for( i = 0; i < n-1; i++)  
  61.     {  
  62.         p = 1;  
  63.         for( j = i+1; j < n; j++)  
  64.         {  
  65.             while(fabs(Cross(ch[j]-ch[i],ch[p+1]-ch[i])) > fabs((Cross(ch[j]-ch[i],ch[p]-ch[i]))))  
  66.                 p = (p+1) % (n-1);  
  67.             ans = max(ans, fabs(Cross(ch[i]-ch[p],ch[j]-ch[p])));  
  68.         }  
  69.         ans = max(ans, fabs(Cross(ch[i]-ch[p],ch[j]-ch[p])));  
  70.   
  71.     }  
  72.   
  73.     return ans/2;  
  74. }  
  75.   
  76.   
  77. int main ()  
  78. {  
  79.   
  80.    // freopen("a.txt","r", stdin);  
  81.     int n;  
  82.     while(scanf("%d", &n) != EOF)  
  83.     {  
  84.         if(n == -1) break;  
  85.   
  86.         for(int i = 0; i < n; i++)  
  87.             scanf("%lf %lf", &p[i].x, &p[i].y);  
  88.   
  89.         int len = ConvexHull(ch, n, p);  
  90.         double ans = rotaing_calipers(ch, len);  
  91.   
  92.         printf("%.2f\n", ans);  
  93.     }  
  94.     return 0;  
  95. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值