POJ 2187 求多邊形直徑(凸包+旋轉卡殼)

題目鏈接:http://poj.org/problem?id=2187


方法一:凸包O(nlogn)+枚舉O(n^2) 

  1. #include<cstdio>  
  2. #include<cmath>  
  3. #include<iostream>  
  4. #include<algorithm>  
  5. using namespace std;  
  6. #define N 51000  
  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. Vector operator - (Point A, Point B) {return Vector(A.x-B.x, A.y-B.y);}  
  14. Point point[N];  
  15. Point convex[N];  
  16.   
  17.   
  18. double dist(Point a, Point b)  
  19. {  
  20.     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);  
  21. }  
  22.   
  23. double Cross(Vector A, Vector B)  
  24. {  
  25.     return A.x*B.y - A.y*B.x;  
  26. }  
  27.   
  28. int cmp(Point a, Point b)  
  29. {  
  30.     if(a.x != b.x) return a.x < b.x;  
  31.     return a.y < b.y;  
  32. }  
  33. int ConvexHull(Point *p, int n, Point *ch)  
  34. {  
  35.     sort(p, p+n, cmp);  
  36.     int m = 0;  
  37.   
  38.     for(int i = 0; i < n; i++)  
  39.     {  
  40.         while(m > 1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <= 0) m--;  
  41.         ch[m++] = p[i];  
  42.     }  
  43.   
  44.     int k = m;  
  45.     for(int i = n-2; i >= 0; i--)  
  46.     {  
  47.         while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;  
  48.         ch[m++] = p[i];  
  49.     }  
  50.     if(n > 1) m--;  
  51.     return m;  
  52. }  
  53. int main()  
  54. {  
  55.     int n;  
  56.     scanf("%d", &n);  
  57.     for(int i = 0; i < n; i++)  
  58.         scanf("%lf %lf", &point[i].x, &point[i].y);  
  59.   
  60.     int len = ConvexHull(point, n, convex);  
  61.   
  62.     double ans = 0;  
  63.     for(int i = 0; i < len; i++)  
  64.         for(int j = i+1; j < len; j++)  
  65.             ans = max(ans, dist(convex[i],convex[j]));  
  66.   
  67.     printf("%.0lf\n", ans);  
  68.   
  69.     return 0;  
  70. }  

方法二:凸包O(nlogn)+旋轉卡殼O(n) 

  1. #include<cmath>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<iostream>  
  5. #include<algorithm>  
  6. using namespace std;  
  7. #define N 50000  
  8. //定義點  
  9. struct Point  
  10. {  
  11.     double x, y;  
  12.     Point(double x = 0, double y = 0) : x(x), y(y) {}  
  13. };  
  14. typedef Point Vector;  
  15. Point p[N+100];  
  16. Point ch[N+100];  
  17. Vector operator - (Point A, Point B) {return Vector(A.x-B.x, A.y-B.y);}  
  18. double Dot(Vector A, Vector B){return A.x*B.x + A.y*B.y;}  
  19. double Length(Vector A){return Dot(A, A);}  
  20. double Cross(Vector A, Vector B){return A.x*B.y - A.y*B.x;}  
  21. bool cmp(Point a, Point b)  
  22. {  
  23.     if(a.x == b.x) return a.y < b.y;  
  24.     else return a.x < b.x;  
  25. }  
  26.   
  27. int ConvexHull(Point p[], int n, Point ch[])  
  28. {  
  29.     sort(p,p+n,cmp);  
  30.     int m = 0;  
  31.     for(int i = 0; i < n; i++)  
  32.     {  
  33.         while(m > 1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <= 0) m--;  
  34.         ch[m++] = p[i];  
  35.     }  
  36.   
  37.     int k = m;  
  38.     for(int i = n-2; i >= 0; i--)  
  39.     {  
  40.         while(m > k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <= 0) m--;  
  41.         ch[m++] = p[i];  
  42.     }  
  43.     if(n > 1) m--;  
  44.     return m;  
  45. }  
  46.   
  47. double slove(Point ch[], int n)  
  48. {  
  49.     int q = 1;  
  50.     double ans = 0;  
  51.     for(int i = 0; i < n-1; i++)  
  52.     {  
  53.         while(Cross(ch[i+1]-ch[i],ch[q]-ch[i]) < Cross(ch[i+1]-ch[i],ch[q+1]-ch[i]))  
  54.             q = (q+1)%n;  
  55.         ans = max(ans, max(Length(ch[q]-ch[i+1]), Length(ch[q]-ch[i])));  
  56.     }  
  57.     return ans;  
  58. }  
  59.   
  60. int main ()  
  61. {  
  62.     //freopen("in.txt", "r", stdin);  
  63.     int n;  
  64.     scanf("%d", &n);  
  65.     for(int i = 0; i < n; i++)  
  66.         scanf("%lf %lf", &p[i].x, &p[i].y);  
  67.   
  68.     int len = ConvexHull(p, n, ch);  
  69.     double ans = slove(ch, len);  
  70.     printf("%.0f\n", ans);  
  71.     return 0;  
  72. }  
發佈了121 篇原創文章 · 獲贊 10 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章