码迷,mamicode.com
首页 > 其他好文 > 详细

uva-10245-分治

时间:2019-01-12 01:05:30      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:ini   color   its   半径   题意   gre   stdin   ndis   ring   

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

根据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\n",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\n",d);
            }
        }
    }

};


int main()
{

#ifndef ONLINE_JUDGE
    freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
    cc::solve();

    return 0;
}

 

uva-10245-分治

标签:ini   color   its   半径   题意   gre   stdin   ndis   ring   

原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/10258327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!