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 ;
    }
  • 相关阅读:
    网络中有三种通讯模式:单播、广播、组播(多播)
    chmod命令
    linux bash 命令重定向和多命令执行
    linux中管道符“|”的作用
    web测试常用的 linux 命令
    集群主要分成三大类 (高可用集群, 负载均衡集群,科学计算集群)
    iOS 监测电话呼入
    iOS NSUserDefaults [setValue:forKey:] [setObject:forKey:] <Objc> setValue(_,forKey:) set(_,forKey) <Swift 3>
    iOS 将navigationItem.titleView设置为自定义UISearchBar (Ficow实例讲解)
    iOS 加载Viewcontroller的几种方法
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3461818.html
Copyright © 2011-2022 走看看