zoukankan      html  css  js  c++  java
  • UVa 10245 The Closest Pair Problem (分治)

    题意:给定 n 个点,求最近两个点的距离。

    析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一个在右边的,我们可以先求全在左边或右边的最小值,假设是d,那么一个点在左边,一个点在右边,那么横坐标之差肯定小于d,才能替换d,同样的纵坐标也是,并且这样的点并不多,然后就可以先选出来,再枚举。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e16;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e4 + 10;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    struct Point{
      double x, y;
      bool operator < (const Point &p) const{
        return x < p.x || x == p.x && y < p.y;
      }
    };
    Point a[maxn];
    
    bool cmp(const Point &lhs, const Point &rhs){
      return lhs.y < rhs.y;
    }
    
    double dfs(Point *a, int n){
      if(n < 2)  return inf;
      int m = n / 2;
      double mid = a[m].x;
      double d = min(dfs(a, m), dfs(a+m, n-m));
      vector<Point> y;
      for(int i = 0; i < n; ++i)
        if(fabs(mid - a[i].x) < d)  y.push_back(a[i]);
    
      sort(y.begin(), y.end(), cmp);
      for(int i = 0; i < y.size(); ++i)
        for(int j = i+1; j < y.size(); ++j){
          if(fabs(y[i].y-y[j].y) >= d)  continue;
          double xx = y[i].x - y[j].x;
          double yy = y[i].y - y[j].y;
          d = min(d, sqrt(xx*xx + yy*yy));
        }
      return d;
    }
    
    int main(){
      while(scanf("%d", &n) == 1 && n){
        for(int i = 0; i < n; ++i)  scanf("%lf %lf", &a[i].x, &a[i].y);
        sort(a, a + n);
        double ans = dfs(a, n);
        if(ans >= 10000.0)  printf("INFINITY
    ");
        else  printf("%.4f
    ", ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    wp8模拟器中使用电脑键盘和模拟器的版本解释
    程序员如何正确的评估自己的薪资
    本地资源之绑定页面的标题和增加软件的语言支持
    C#导出数据的EXCEL模板设计
    程序员高效编程的14点建议
    使用StaticResource给控件定义公共的样式和属性来写界面XAML
    程序员什么时候该考虑辞职
    我的第一个wp8小程序
    检测CPU是否支持虚拟化
    所有经历都是一种恩赐
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7285059.html
Copyright © 2011-2022 走看看