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 ;
    }
  • 相关阅读:
    做题记录
    关于有向图强连通分量的一点想法
    浅谈二分图匹配(未完)
    水题狂欢赛 (爬楼梯赛)题解(偏向自我反省)
    浅谈迭代加深(iddfs)
    浅谈单调队列优化
    [cqbzoj#10644]鱼肉炸弹题解
    树形背包[2/ 50] luogu [P1273]
    树形背包[1/ 50] luogu [P2015] (超级板)
    (树状数组)区间修改,区间查询
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3461818.html
Copyright © 2011-2022 走看看