hdu 1007 Quoit Design

本文详细解析了一种基于分治算法解决最小点距离问题的方法,该问题旨在寻找平面上两点间最小距离。通过实例演示了如何利用分治策略优化算法效率,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Quoit Design

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


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:   1006  1009  1005  1008  1004 
/*
  最小点距离问题 
  采用分治算法,假设对于1-n的区间,我们已经求出1~m(m是中点)和m+1~n的结果分别是d1和d2,
  那么如果1~n的答案出现在这两个单独的区间内,很明显就是min(d1,d2),否则在两个区间之间产生。
  如果直接两重循环枚举两个区间的数会T,所以考虑优化:
  ①:如果某个点到m的距离大于min(d1,d2),那么不考虑。
  ②:首先用到一个结论:
        假设有一个点q,坐标是xq, yq。可以证明在以q为底边中点,长为2d,宽为d的矩形区域内不会有超过6个点
        (证明见算法导论,然而我并没看懂,Orz)
      有了这个结论之后,我们将第一次优化后的点按照y排序,对于一个点i,如果某个点j与i的y坐标之差大于之前求出的ans,
      那么j之后的就不用计算了。(不是很明白复杂度的证明) 
*/
#include<cstdio>
#include<cmath>
#include<algorithm>
#define pf(x) ((x)*(x))
using namespace std;
const int N=1e5+10;
struct node{
    double x,y;
}qx[N],qy[N];
int n;
bool cmpx(const node &a,const node &b){
    return a.x<b.x;
}
bool cmpy(const node &a,const node &b){
    return a.y<b.y;
}
double getdis(const node &a,const node &b){
    return sqrt(pf(a.x-b.x)+pf(a.y-b.y));
}
double solve(int l,int r){
    if(l+1==r) return getdis(qx[l],qx[r]);
    if(l+2==r) return min(getdis(qx[l],qx[r]),min(getdis(qx[l],qx[l+1]),getdis(qx[l+1],qx[r])));
    int mid=l+r>>1,cnt=0;
    double ans=min(solve(l,mid),solve(mid+1,r));
    for(int i=l;i<=r;i++){
        if(fabs(qx[i].x-qx[mid].x)<=ans){
            qy[++cnt]=qx[i];
        }
    }
    sort(qy+1,qy+cnt+1,cmpy);
    for(int i=1;i<=cnt;i++){
        for(int j=i+1;j<=cnt;j++){
            if(qy[j].y-qy[i].y>=ans) break;
            ans=min(ans,getdis(qy[i],qy[j]));
        }
    }
    return ans;
}
void work(){
    for(int i=1;i<=n;i++) scanf("%lf%lf",&qx[i].x,&qx[i].y);
    sort(qx+1,qx+n+1,cmpx);
    printf("%.2lf\n",solve(1,n)/2);
}
int main(){
    while(scanf("%d",&n)==1&&n) work();
    return 0;
} 

 

转载于:https://www.cnblogs.com/shenben/p/6283249.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值