zoukankan      html  css  js  c++  java
  • 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 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
    ",Divide_conquer(0,n-1)/2);
        }
        return  0;
    }
    




  • 相关阅读:
    jquery validate 自定义验证方法 日期验证
    转:前端控制器是邪恶的么?
    eclipse打开文件位置Open Explorer 插件
    Eclipse标准版安装J2EE
    短网址生成思路算法
    smarty模板执行原理
    Smarty 变量使用
    Php模板引擎Smarty安装和配置
    转:PHP分页技术的代码和示例
    黑板架构模式及其常见实现方式
  • 原文地址:https://www.cnblogs.com/lccurious/p/5079880.html
Copyright © 2011-2022 走看看