zoukankan      html  css  js  c++  java
  • uva-10245-分治

    题意:数组二维空间内的点,求最近的俩个点的距离.

    根据x排序,求左部分的最近距离,右部分最近距离,然后以中点,当前距离为半径,计算所有的点距离.

    #include <string>
    #include<iostream>
    #include<map>
    #include<memory.h>
    #include<vector>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<math.h>
    #include<iomanip>
    #include<bitset>
    #include"math.h"
    #include "stdio.h"
    namespace cc
    {
        using std::cout;
        using std::endl;
        using std::cin;
        using std::map;
        using std::vector;
        using std::string;
        using std::sort;
        using std::priority_queue;
        using std::greater;
        using std::vector;
        using std::swap;
        using std::stack;
        using std::bitset;
    
        class Node
        {
        public:
            double x;
            double y;
            Node() {};
            Node(double x, double y) :x(x), y(y) {};
    
        };
    
        constexpr int N = 10000;
        Node a[N + 1];
    
        
    
    
    
        bool cmp(Node& a, Node& b)
        {
            return a.x < b.x;
        }
    
        double dist(Node a, Node b)
        {
            return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
        }
    
        double minDist(int l, int r)
        {
            if (l > r) return 40004;
            int mid = (l + r) / 2;
            int s = mid, e = mid;
            double d = std::min(minDist(l, mid - 1), minDist(mid + 1, r));
            while (s >= l && d > a[mid].x - a[s].x) s--;
            while (e <= r && d >  a[e].x- a[mid].x) e++;
            for (int i = s + 1;i < e;i++)
            {
                for (int j = i + 1;j < e;j++)
                {
                    d = std::min(dist(a[i], a[j]), d);
                }
            }
            return d;
        }
    
        void solve()
        {
    
            double s, e;
            int n;
            while (cin >> n && n)
            {
                for (int i = 0;i < n;i++)
                {
                    cin >> s >> e;
                    Node node(s, e);
                    a[i] = node;
                }
                sort(a, a + n, cmp);
                double d = minDist(0,n-1);
                if (d >= 10000)
                {
                    cout << "INFINITY" << endl;
                    
                }
                else
                {
                    printf("%.4lf
    ",d);
                }
            }
        }
    
    };
    
    
    int main()
    {
    
    #ifndef ONLINE_JUDGE
        freopen("d://1.text", "r", stdin);
    #endif // !ONLINE_JUDGE
        cc::solve();
    
        return 0;
    }

    另外,这个题裸奔也行的

    #include <string>
    #include<iostream>
    #include<map>
    #include<memory.h>
    #include<vector>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<math.h>
    #include<iomanip>
    #include<bitset>
    #include"math.h"
    #include "stdio.h"
    namespace cc
    {
        using std::cout;
        using std::endl;
        using std::cin;
        using std::map;
        using std::vector;
        using std::string;
        using std::sort;
        using std::priority_queue;
        using std::greater;
        using std::vector;
        using std::swap;
        using std::stack;
        using std::bitset;
    
        class Node
        {
        public:
            double x;
            double y;
            Node() {};
            Node(double x, double y) :x(x), y(y) {};
    
        };
    
        constexpr int N = 10000;
        Node a[N + 1];
    
        
    
    
    
        bool cmp(Node& a, Node& b)
        {
            return a.x < b.x;
        }
    
        double dist(Node a, Node b)
        {
            return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
        }
    
        double minDist(int l, int r)
        {
            if (l > r) return 40004;
            int mid = (l + r) / 2;
            int s = mid, e = mid;
            double d = std::min(minDist(l, mid - 1), minDist(mid + 1, r));
            while (s >= l && d > a[mid].x - a[s].x) s--;
            while (e <= r && d >  a[e].x- a[mid].x) e++;
            for (int i = s + 1;i < e;i++)
            {
                for (int j = i + 1;j < e;j++)
                {
                    d = std::min(dist(a[i], a[j]), d);
                }
            }
            return d;
        }
    
        void solve()
        {
    
            double s, e;
            int n;
            while (cin >> n && n)
            {
                for (int i = 0;i < n;i++)
                {
                    cin >> s >> e;
                    Node node(s, e);
                    a[i] = node;
                }
                sort(a, a + n, cmp);
                double d = 400000;
                for (int i = 0;i < n;i++)
                {
                    for (int j = i + 1;j < n;j++)
                    {
                        d = std::min(dist(a[i],a[j]),d);
                    }
                }
                if (d >= 10000)
                {
                    cout << "INFINITY" << endl;
                    
                }
                else
                {
                    printf("%.4lf
    ",d);
                }
            }
        }
    
    };
    
    
    int main()
    {
    
    #ifndef ONLINE_JUDGE
        freopen("d://1.text", "r", stdin);
    #endif // !ONLINE_JUDGE
        cc::solve();
    
        return 0;
    }
  • 相关阅读:
    LeetCode 1122. Relative Sort Array (数组的相对排序)
    LeetCode 46. Permutations (全排列)
    LeetCode 47. Permutations II (全排列 II)
    LeetCode 77. Combinations (组合)
    LeetCode 1005. Maximize Sum Of Array After K Negations (K 次取反后最大化的数组和)
    LeetCode 922. Sort Array By Parity II (按奇偶排序数组 II)
    LeetCode 1219. Path with Maximum Gold (黄金矿工)
    LeetCode 1029. Two City Scheduling (两地调度)
    LeetCode 392. Is Subsequence (判断子序列)
    写程序判断系统是大端序还是小端序
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/10258327.html
Copyright © 2011-2022 走看看