现在的位置: 首页 > 综合 > 正文

POJ2187(旋转卡壳求平面最远点对) POJ2187(旋转卡壳求平面最远点对)

2017年11月04日 ⁄ 综合 ⁄ 共 1679字 ⁄ 字号 评论关闭
 

POJ2187(旋转卡壳求平面最远点对)

分类: 计算几何 62人阅读 评论(0) 收藏 举报

旋转卡壳可以用于求凸包的直径、宽度,两个不相交凸包间的最大距离和最小距离等。深度学习旋转卡壳请点击这里

题目:Beauty Contest

  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <stdio.h>  
  4.   
  5. using namespace std;  
  6.   
  7. const int N=50005;  
  8.   
  9. class Point  
  10. {  
  11.     public:  
  12.        int x;  
  13.        int y;  
  14.        bool operator < (const Point &P) const  
  15.        {  
  16.            return y<P.y||(y==P.y&&x<P.x);  
  17.        }  
  18. };  
  19.   
  20. Point stack[N];  
  21. Point p[N];  
  22.   
  23. int len;  
  24.   
  25. int dist(Point A,Point B)  
  26. {  
  27.     return ((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));  
  28. }  
  29.   
  30. int cross(Point A,Point B,Point C)  
  31. {  
  32.     return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x);  
  33. }  
  34.   
  35. void Graham(int n)  
  36. {  
  37.      sort(p,p+n);  
  38.      stack[0]=p[0];  
  39.      stack[1]=p[1];  
  40.      int top=1;  
  41.      for(int i=2;i<n;i++)  
  42.      {  
  43.          while(top>0 && cross(stack[top-1],stack[top],p[i])<=0) top--;  
  44.          stack[++top]=p[i];  
  45.      }  
  46.      int temp=top;  
  47.      for(int i=n-2;i>=0;i--)  
  48.      {  
  49.          while(top>temp && cross(stack[top-1],stack[top],p[i])<=0) top--;  
  50.          stack[++top]=p[i];  
  51.      }  
  52.      len=top;  
  53. }  
  54.   
  55. int rotating_calipers(int n)  
  56. {  
  57.     int k=1,ans=0,i;  
  58.     stack[n]=stack[0];  
  59.     for(i=0;i<n;i++)  
  60.     {  
  61.         while(cross(stack[i],stack[i+1],stack[k+1])>cross(stack[i],stack[i+1],stack[k]))  
  62.             k=(k+1)%n;  
  63.         ans=max(ans,max(dist(stack[i],stack[k]),dist(stack[i+1],stack[k+1])));  
  64.     }  
  65.     return ans;  
  66. }  
  67.   
  68. int main()  
  69. {  
  70.     int n,i;  
  71.     while(~scanf("%d",&n))  
  72.     {  
  73.          for(i=0;i<n;i++)  
  74.              scanf("%d%d",&p[i].x,&p[i].y);  
  75.          len=0;  
  76.          Graham(n);  
  77.          printf("%d\n",rotating_calipers(len));  
  78.     }  
  79.     return 0;  
  80. }  

抱歉!评论已关闭.