hdu1007 Quoit Design (分治+鸽舍原理)

Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 37004    Accepted Submission(s): 9612


Problem Description
Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.

Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.
 

Input
The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.
 

Output
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places. 
 

Sample Input
  
  
2 0 0 1 1 2 1 1 1 1 3 -1.5 0 0 0 0 1.5 0
 

Sample Output
  
  
0.71 0.00 0.75
 

Author
CHEN, Yue
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1009  1008  1010  1011  1024 
 
ps:下面这个排序的算法是错误的,我就不删除了,引以为戒。我在排序算法的下方给出了分治的解法。

  解析:这道题看了网上的很多题解,全是用分治法找中位数求解,个人表示没看懂?我觉得好像没必要搞这么复杂吧。
             好了,下面说我的做法,如果有人知道为什么这道题一定要用分治法找中位数求解,请务必告诉我,我的qq号在博客上方疾就有。
           思路解析:
               1. 我们考虑这样一个问题,对于点(x,y),如何寻找与它最近的点?假设有两个点(a,b)、(c、d),则有:
                          len1^2=(x-a)^2+(y-b)^2
                          len2^2=(x-c)^2+(y-d)^2
                  若点(a,b)距离点(x,y)更近,那么必有:(|x-a|<|x-c| )   || (|y-b|<|y-d|)==1
                  即要么点(a,b )在 x 轴上与点(x,y)更近,要么点(a,b )在 y 轴上与点(x,y)更近,或者两者都满足。
                  
                  那么如何求解在 n 个点钟,求解与它最近的点的距离呢?
                  首先,按照每个点 x 值由小到大进行排序,若 x 相同,则按照 y 由小到大进行排序,i 号点处在第 k 个位置上,则 |x-a| 尽可能小的情况下,与 i 点的最小距离为 length(k,k+1)、length(k-1,k)的最小值,设其值为 len1。
                  然后,按照每个点 y 值由小到大进行排序,若 y 相同,则按照 x 由小到大进行排序,i 号点处在第 k 个位置上,则 |y-b| 尽可能小的情况下,与 i 点的最小距离为 length(k,k+1)、length(k-1,k)的最小值,设其值为 len2。
                  
                  则根据最开始的分析,与第 i 点的最小距离为 min(len1,len2)。
               
                2.具体实现:
                   ①按照每个点 x 值由小到大进行排序,若 x 相同,则按照 y 由小到大进行排序,计算相邻两点的最短距离,即为len1。

                   ② 按照每个点 y 值由小到大进行排序,若 y 相同,则按照 x 由小到大进行排序,计算相邻两点的最短距离,即为len2。
                  答案即为:min(len1,len2)。
代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
#define maxn 100000
using namespace std;

struct tnode{double x,y;}q[maxn+100];

bool cmp1(tnode a,tnode b)
{
  if(a.x<b.x)return 1;
  if(a.x>b.x)return 0;
  return a.y<b.y;
}

bool cmp2(tnode a,tnode b)
{
  if(a.y<b.y)return 1;
  if(a.y>b.y)return 0;
  return a.x<b.x;
}

double length(int i,int j)
{
  double x,y;
  x=(q[i].x-q[j].x)*(q[i].x-q[j].x);
  y=(q[i].y-q[j].y)*(q[i].y-q[j].y);
  return sqrt(x+y);
}

int main()
{
  freopen("1.in","r",stdin);
  int n,i,j,k;
  double len;
  while(scanf("%d",&n),n)
    {
      for(i=1;i<=n;i++)scanf("%lf%lf",&q[i].x,&q[i].y);
      sort(q+1,q+n+1,cmp1);
      len=length(1,2);
      for(i=2;i<n;i++)len=min(len,length(i,i+1));
      sort(q+1,q+n+1,cmp2);
      for(i=1;i<n;i++)len=min(len,length(i,i+1));
      printf("%.2lf\n",len/2);
    }
  return 0;
}

解析:分治法求解。
           分治法求最近点对,网上有很多资料,这里我就不再细说了。
                  这篇文章有关于分治求最近点对的算法讲解,主要看他的合并那里的讲解。(分治求最近点对其实不难,只是合并那里很难想)
代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
#define maxn 100000
#define inf 2000000000
#define jd 0.000001
using namespace std;

int n;
double ans;
struct tnode{double x,y;}d[maxn+20];
int q[maxn],p[maxn];

bool cmp1(tnode a,tnode b)
{
  if(a.x<b.x)return 1;
  if(a.x>b.x)return 0;
  return a.y<b.y;
}

bool cmp2(int a,int b)
{
  return d[a].y<d[b].y;
}

double get_length(int a,int b)
{
  double x1,x2;
  x1=d[a].x-d[b].x;
  x2=d[a].y-d[b].y;
  return sqrt(x1*x1+x2*x2);
}

void get(int l,int r)
{
  if(l==r)return;
  if(l+1==r)
    {
      ans=min(ans,get_length(l,r));
      return;
	}
  
  int i,j,k,mid=(l+r)/2;
  get(l,mid),get(mid+1,r);
  double xmid=(d[mid].x+d[mid+1].x)/2; 
  for(q[0]=0,i=mid;i>=l;i--)
    if(xmid-d[i].x<ans)q[++q[0]]=i;
    else break;
  for(p[0]=0,i=mid+1;i<=r;i++)
    if(d[i].x-xmid<ans)p[++p[0]]=i;
    else break;
    
  sort(q+1,q+q[0]+1,cmp2);
  sort(p+1,p+p[0]+1,cmp2);
  
  i=j=1;
  while(i<=q[0] && j<=p[0])
    {
      while(i<=q[0] && j<=p[0] && fabs(d[q[i]].y-d[p[j]].y)>=ans)
        {
          if(d[q[i]].y<=d[p[j]].y)i++;
          else j++;
		}
      if(i>q[0] || j>p[0])return;
      
	  for(k=j;k<j+6 && k<=p[0];k++)
        ans=min(ans,get_length(q[i],p[k]));
      i++;
	}
}

int main()
{
  int i;
  while(scanf("%d",&n),n!=0)
    {
      for(i=1;i<=n;i++)scanf("%lf%lf",&d[i].x,&d[i].y);
      sort(d+1,d+n+1,cmp1);
      for(i=2;i<=n;i++)
        if(fabs(d[i].x-d[i-1].x)<=jd && fabs(d[i].y-d[i-1].y)<=jd)break;
      if(i<=n){printf("0.00\n");continue;}
	  ans=inf,get(1,n);
      printf("%.2lf\n",ans/2);
	}
  return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值