HDU 1007 Quoit Design竟然要分治

简介: Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34742    Accepted Submis...


Quoit Design

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

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:   1008  1010  1011  1024  1016 
 


说实话,这道题真的很让我崩溃,刚开始看题目的时候觉得只要把各个点记录下来,然后按照横坐标和纵坐标来排序就好了,但是提交之后是WA,然后考虑了很久,觉得这样排序并没有什么不妥,但是突然发现其实这离答案还差的远,因为如果是A、B、C三个已经按照横坐标顺序排好序的点,如果AB的纵坐标相差非常之大,那么还是可能和C点的距离比较近。后来翻阅到编程之美中的文章有关介绍最近点对的问题的,给出了一维情况下的代码,二维情况时有思维讲解,即用分治的思路去考虑。

然而这里还有一点要进行证明以完成剪枝。即两点间的横坐标之间的距离差一定是小于等于它们之间直接的距离之差的,所以每次在进行区间的判断的时候可以只是先按照这个计算量小的方式进行判断,最后在那个不能再细分的区间里按照纵坐标再间接排序一次,然后暴力求解。

代码君:
#include<cstdio>
#include<algorithm>
#include<cmath>
#define maxn 1100010
#define INF 214748364
using   namespace   std;
int q[maxn];
struct  Point{
    double  x,y;
};
bool    cmp_x(Point a,Point b){
    return  a.x < b.x;
}
Point   arry[maxn];
bool    cmp_y(int   a,int   b){
    return  arry[a].y < arry[b].y;
}
double  calc(Point  a,Point b){
    return  sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
double  Divide_conquer(int l,int r){
    if(l >= r)
        return INF;
    int n = 0;
    int m = (l+r)>>1;
    double  a = min(Divide_conquer(l,m),Divide_conquer(m+1,r));
    for(int i = m;i >= l;--i){
        if(arry[m].x - arry[i].x < a)
            q[n++] = i;
        else
            break;
    }
    for(int i = m+1;i <= r;++i){
        if(arry[i].x - arry[m].x < a)
            q[n++] = i;
        else
            break;
    }
    sort(q, q+n, cmp_y);
    for(int i = 0;i < n;i++){
        for(int j = i+1;j < n;j++){
            if(arry[q[j]].y - arry[q[i]].y < a)
                a = min(a,calc(arry[q[i]],arry[q[j]]));
            else
                break;
        }
    }
    return  a;
}
int main(){
    int n;
    while(~scanf("%d",&n) && n){
        for(int i = 0;i < n;i++){
            scanf("%lf%lf",&arry[i].x,&arry[i].y);
        }
        sort(arry,arry+n,cmp_x);
        printf("%.2f\n",Divide_conquer(0,n-1)/2);
    }
    return  0;
}




 

相关文章
|
10天前
【洛谷 P1219】[USACO1.5]八皇后 Checker Challenge 题解(深度优先搜索+回溯法)
**USACO1.5八皇后挑战**是关于在$n\times n$棋盘上放置棋子的,确保每行、每列及两条主对角线上各有一个且仅有一个棋子。给定$6$作为输入,输出前$3$个解及解的总数。例如,对于$6\times6$棋盘,正确输出应包括解的序列和总数。代码使用DFS解决,通过跟踪对角线占用状态避免冲突。当找到所有解时,显示前三个并计数。样例输入$6$产生输出为解的前三个排列和总数$4$。
10 0
|
9月前
|
算法 IDE Java
【洛谷算法题】P1001-A+B Problem【入门1顺序结构】
【洛谷算法题】P1001-A+B Problem【入门1顺序结构】
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
125 0
|
Java 物联网 Go
洛谷 P1001 A+B Problem (java实现)
洛谷 P1001 A+B Problem (java实现)
|
算法 BI
约瑟夫问题(Josephus problem)的klog(n)解法
约瑟夫问题是一个经典的算法问题,其解决过程涉可能用到的数据结构有数组、链表,涉及的算法包括模拟、递归、递推、动态规划等等,因此非常适合做一道面试题。   问题描述: 首先n个候选人围成一个圈,依次编号为0..n-1。然后指定一个正整数k,并0号候选人开始按从1到k的顺序依次报数,n-1号候选人报数之后,又再次从0开始。当有人报到K时,这个人被淘汰,从圈里出去。下一个人从1开始重新报
4321 0
|
人工智能 BI 算法