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\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;
}

 

posted on 2019-01-12 00:30 好吧,就是菜菜 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/10258327.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值