zoukankan      html  css  js  c++  java
  • UVA 10245

    Problem J
    The Closest Pair Problem
    Input: standard input
    Output: standard output
    Time Limit: 8 seconds
    Memory Limit: 32 MB

    Given a set of points in a two dimensional space, you will have to find the distance between the closest two points.

    Input

    The input file contains several sets of input. Each set of input starts with an integer N (0<=N<=10000), which denotes the number of points in this set. The next N line contains the coordinates of N two-dimensional points. The first of the two numbers denotes the X-coordinate and the latter denotes the Y-coordinate. The input is terminated by a set whose N=0. This set should not be processed. The value of the coordinates will be less than 40000 and non-negative.

    Output

    For each set of input produce a single line of output containing a floating point number (with four digits after the decimal point) which denotes the distance between the closest two points. If there is no such two points in the input whose distance is less than 10000, print the line INFINITY.

     

    Sample Input

    3
    0 0
    10000 10000
    20000 20000
    5
    0 2
    6 67
    43 71
    39 107
    189 140
    0

     

    Sample Output

    INFINITY
    36.2215

      很经典的算法,翻开任何一本算法书籍,分治算法那一节 。  

      STL的接口都是左闭又开 。  [)  。

      合并的地方用到了  原地归并排序(上知网找) 。

     

    #include <iostream>
    #include <string>
    #include <string.h>
    #include <map>
    #include <stdio.h>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <math.h>
    #include <set>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std ;
    typedef long long LL ;
    const int Max_N = 10008 ;
    const double INF = 100000.0 ;
    typedef pair<double ,double> P ;
    bool cmp_y(P A ,P B){
         return A.second < B.second ;
    }
    int N ;
    P A[Max_N] ;
    double close_pair(P *a ,int n){
        if(n == 1)
          return INF ;
        int m = n>>1 ;
        int x = a[m].first ;
        double dist = min(close_pair(a,m),close_pair(a+m,n-m)) ;
        inplace_merge(a,a+m,a+n,cmp_y) ;   // 原地归并排序
        vector<P>vec ;
        for(int i = 0 ; i < n ; i++){
            if(fabs(a[i].first - x)>=dist)
                continue ;
            for(int j = vec.size() - 1 ; j >= 0 ; j--){
                double dx = a[i].first - vec[j].first ;
                double dy = a[i].second - vec[j].second ;
                if(dy >= dist)
                   break ;
                dist = min(dist,sqrt(dx*dx + dy*dy)) ;
            }
            vec.push_back(a[i]) ;
        }
        return dist ;
    }
    int main(){
       int T ;
       while(scanf("%d",&N)&&N){
            for(int i = 0 ; i < N ; i++)
                scanf("%lf%lf",&A[i].first,&A[i].second) ;
            sort(A,A+N) ;
            double dist = close_pair(A,N) ;
            if(dist - 10000 <= 1e-9)
               printf("%.4lf
    ",close_pair(A , N)) ;
            else
               puts("INFINITY") ;
       }
       return 0 ;
    }
  • 相关阅读:
    SQL字符串操作汇总
    重构之道清除代码异味
    Html.Action和Html.RederAction来创建子视图
    C#实现Thrift连接池[新]
    CentOS下配置Apache反向代理出错的解决
    entity framework实体用数据库默认值的方法
    为IEnumerable类型添加Add方法
    一个对Entity Framework数据层的封装
    将.netFramework4.5/MVC4/EF5/Oracle网站发布到Server2008/iis7的痛苦经历
    让vs2012运行vs2010插件的方法
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3461818.html
Copyright © 2011-2022 走看看