Quoit Design - HDU 1007 分治

本文解析了一个名为QuoitDesign的游戏算法题,该题目要求玩家找到能够恰好套住一个玩具的最大环的半径。文章提供了详细的输入输出样例及AC代码实现。

Quoit Design

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


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
 

思路:新手上路,鉴于我的理解不够深刻就不误导大家了,就只提供下代码吧。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{ double x,y;
}dian[100010];
int a[100010];
bool cmpx(node a,node b)
{ return a.x<b.x;}
bool cmpy(int a,int b)
{ return dian[a].y<dian[b].y;}
double dis(int i,int j)
{ return sqrt((dian[i].x-dian[j].x)*(dian[i].x-dian[j].x)+(dian[i].y-dian[j].y)*(dian[i].y-dian[j].y));}
double fen(int l,int r)
{ int i,j,num=0;
  if(l+1==r)
   return dis(l,r);
  if(l+2==r)
   return min(dis(l,r),min(dis(l,l+1),dis(l+1,r)));
  int mi=(l+r)/2;
  double ans=min(fen(l,mi),fen(mi+1,r));
  for(i=l;i<=r;i++)
    if(fabs(dian[i].x-dian[mi].x)<=ans)
     a[++num]=i;
  sort(a+1,a+1+num,cmpy);
  for(i=1;i<=num;i++)
   for(j=i+1;j<=num;j++)
   { if(dian[a[j]].y-dian[a[i]].y>=ans)
      break;
     ans=min(ans,dis(a[i],a[j]));
   }
  return ans;
}
int main()
{ int n,i,j,k;
  while(~scanf("%d",&n) && n)
  { for(i=1;i<=n;i++)
     scanf("%lf%lf",&dian[i].x,&dian[i].y);
    sort(dian+1,dian+1+n,cmpx);
    printf("%.2f\n",fen(1,n)/2);
  }
}




Quoit Design”通常指的是一道经典的算法竞赛题目。该问题的核心是在平面上给定一系列的点,需要找出其中距离最近的两个点之间距离的一半,这个距离可以类比为游戏中套圈的半径设计。 ### 解决思路与步骤 1. **排序**:首先将所有点按照 $x$ 坐标进行排序。 2. **分治**:将排序后的点集从中间分成左右两部分,分别递归地在左右两部分中找出最近点对的距离 $d_1$ 和 $d_2$,并取 $d = \min(d_1, d_2)$。 3. **合并**:考虑跨越左右两部分的点对。找出所有 $x$ 坐标距离中间分割线不超过 $d$ 的点,将这些点按照 $y$ 坐标排序。然后遍历这些点,对于每个点,只需要检查它后面与它 $y$ 坐标差值不超过 $d$ 的点对,更新最小距离。 ### 示例代码(Python 实现) ```python import math def distance(p1, p2): return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2) def closest_pair(points): n = len(points) if n <= 3: min_dist = float('inf') for i in range(n): for j in range(i + 1, n): dist = distance(points[i], points[j]) if dist < min_dist: min_dist = dist return min_dist mid = n // 2 mid_point = points[mid] left_points = points[:mid] right_points = points[mid:] d_left = closest_pair(left_points) d_right = closest_pair(right_points) d = min(d_left, d_right) strip = [] for point in points: if abs(point[0] - mid_point[0]) < d: strip.append(point) strip.sort(key=lambda point: point[1]) for i in range(len(strip)): for j in range(i + 1, min(i + 7, len(strip))): dist = distance(strip[i], strip[j]) if dist < d: d = dist return d # 示例使用 points = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] points.sort() result = closest_pair(points) print(result / 2) ``` ### 复杂度分析 - **时间复杂度**:$O(n \log n)$,其中 $n$ 是点的数量。排序操作的时间复杂度是 $O(n \log n)$,分治和合并的过程也是 $O(n \log n)$。 - **空间复杂度**:$O(n)$,主要用于存储点集和递归调用栈。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值