hunnu11323(在n个点中,选择两个使得它们之间的距离最大)

解题思路:

1、求n个点的凸包,因为那两个点一定在凸包上;

2、暴力求解,如果题目时间卡的紧的话,会超时;所以我们用旋转卡壳法;

代码如下:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define  N 1005
#define eps 1e-9
#define pi acos(-1.0)
#define P system("pause")
using namespace std;
struct node
{
       int id;
       double x,y;       
}p[N],ch[N];
int xx,yy;
node operator - (node a,node b)
{
     a.x -= b.x;
     a.y -= b.y;
     return a;
}
double cross(node a, node b)
{
       return a.x*b.y - a.y*b.x;        
}
bool cmp(node a,node b)
{
     if(a.x != b.x)
        return a.x < b.x;
     if(a.y != b.y)
        return a.y < b.y; 
     return a.id < b.id;    
}
double dist(node a,node b)
{
       return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));       
}
int convexhull(int n)
{
    sort(p,p+n,cmp);  
    int i,m = 0;
    int c = 1;
    for(i = 1; i < n; i++)
    {
        if(p[i].x != p[i-1].x || p[i].y != p[i-1].y)
            p[c++] = p[i];        
    }
    n = c;
    for(i=0; i1 && cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) 
                   m--;
         ch[m++] = p[i];              
    }  
    int k = m;
    for(i = n-2; i >=0; i--)
    {
          while(m > k && cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0)
                  m--;
          ch[m++] = p[i];      
    }
    if(n > 1) m--;
    return m;
}

double rotating_calipers(int m)
{
    xx = ch[0].id;
    yy = ch[1].id;
    int q = 1;
    double ans = 0;
    ch[m] = ch[0];
    int i;
    for(i = 0; i < m; i++)
    {
          while(cross(ch[i+1]-ch[i],ch[q+1]-ch[i]) > cross(ch[i+1]-ch[i],ch[q]-ch[i]) )
             q = (q+1)%m;
          if(dist(ch[i],ch[q]) > ans)
          {
              xx = ch[i].id; yy = ch[q].id;    
              ans = dist(ch[i],ch[q]);                 
          }
          if(dist(ch[i+1],ch[q+1]) > ans)
          {
              xx = ch[i+1].id; yy = ch[q+1].id;                          
              ans = dist(ch[i+1],ch[q+1]); 
          }        
    }     
    return ans;
}

int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
    int n,z=1;
    while(scanf("%d",&n) && n)
    {
          int i;
          for(i = 0; i < n; i++){
               scanf("%lf%lf",&p[i].x,&p[i].y);
               p[i].id = i;
          }
          
          int top = convexhull(n);
      //    for(i = 0; i < top; i++)
        //     cout<


你可能感兴趣的:(acm之计算几何)