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

解题思路:

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

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

代码如下:

#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#include<math.h>

#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; i<n; i++)
    {
         while(m>1 && 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<<ch[i].x<<" "<<ch[i].y<<" "<<ch[i].id<<endl;
          rotating_calipers(top);        
          if(yy < xx)
          {
              i = yy; yy = xx; xx = i;      
          }                                    
          printf("Case %d: %d %d\n",z++,xx,yy);
    }                                        

  //  P;                               
    return 0;    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值