VJ传送门

简要题意:给出两个大小均为\(N\)的点集\(A,B\),试在\(A\)中选择一个点,在\(B\)中选择一个点,使得它们在所有可能的选择方案中欧几里得距离最小,求出这个距离


下面给出的两种解法基本上都能够被卡成\(O(n^2)\)……

按照平面最近点对的做法去做,只是在贡献答案的时候加上所属点集不同的限制就可以了。

当然这个可以卡,只要把\(A\)、\(B\)集合之间分得很开,而\(A\)集合和\(B\)集合内部的点两两之间的距离很小,这样在分治下去的过程中没法贡献答案,最后在分治的第一层就有可能会退化成\(O(n^2)\)

如果你愿意可以旋转坐标系来部分解决上面的问题

代码没有写

K-D Tree

把\(A\)集合的点全部加进去构建K-D Tree,对于\(B\)集合内的每个点在K-D Tree上搜索,加个最优化剪枝。

这个怎么卡应该不需要说了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<vector>
#include<cmath>
#define ld long double
#define int long long
//This code is written by Itst
using namespace std;

inline int read(){
    int a = 0;
    char c = getchar();
    bool f = 0;
    while(!isdigit(c) && c != EOF){
        if(c == '-')
            f = 1;
        c = getchar();
    }
    if(c == EOF)
        exit(0);
    while(isdigit(c)){
        a = a * 10 + c - 48;
        c = getchar();
    }
    return f ? -a : a;
}

const int MAXN = 1e5 + 7;
struct point{
    int x , y , ind;
    point(int _x = 0 , int _y = 0 , int _i = 0):x(_x) , y(_y) , ind(_i){}
}P[MAXN];
int N , rt;
int ch[MAXN][2] , X[MAXN][2] , Y[MAXN][2] , p[MAXN][2];
ld ans;

bool cmp1(point a , point b){
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}

bool cmp2(point a , point b){
    return a.y == b.y ? a.x < b.x : a.y < b.y;
}

inline ld calc(point a , point b){
    return sqrt((long double)(a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

inline void merge(int x , int y){
    X[x][0] = min(X[x][0] , X[y][0]);
    X[x][1] = max(X[x][1] , X[y][1]);
    Y[x][0] = min(Y[x][0] , Y[y][0]);
    Y[x][1] = max(Y[x][1] , Y[y][1]);
}

int build(int l , int r , bool f){
    if(l > r)
        return 0;
    int mid = (l + r) >> 1;
    nth_element(P + l , P + mid , P + r + 1 , f ? cmp1 : cmp2);
    int t = P[mid].ind;
    X[t][0] = X[t][1] = P[mid].x;
    Y[t][0] = Y[t][1] = P[mid].y;
    if(ch[t][0] = build(l , mid - 1 , f ^ 1))
        merge(t , ch[t][0]);
    if(ch[t][1] = build(mid + 1 , r , f ^ 1))
        merge(t , ch[t][1]);
    return t;
}

inline ld qw(int x , point p){
    int mx = max(max(X[x][0] - p.x , p.x - X[x][1]) , 0ll) , my = max(max(Y[x][0] - p.y , p.y - Y[x][1]) , 0ll);
    return sqrt((long double)mx * mx + my * my);
}

void dfs(int x , point q , bool f){
    if(x == 0 || qw(x , q) > ans)
        return;
    ans = min(ans , calc(point(p[x][0] , p[x][1]) , q));
    if(f ? cmp1(point(p[x][0] , p[x][1]) , q) : cmp2(point(p[x][0] , p[x][1]) , q)){
        dfs(ch[x][1] , q , f ^ 1);
        dfs(ch[x][0] , q , f ^ 1);
    }
    else{
        dfs(ch[x][0] , q , f ^ 1);
        dfs(ch[x][1] , q , f ^ 1);
    }
}

signed main(){
#ifndef ONLINE_JUDGE
    freopen("in","r",stdin);
    freopen("out","w",stdout);
#endif
    for(int T = read() ; T ; --T){
        N = read();
        for(int i = 1 ; i <= N ; ++i){
            P[i].x = p[i][0] = read();
            P[i].y = p[i][1] = read();
            P[i].ind = i;
        }
        rt = build(1 , N , 0);
        ans = 1e50;
        for(int i = 1 ; i <= N ; ++i){
            P[0].x = read();
            P[0].y = read();
            dfs(rt , P[0] , 0);
        }
        cout << fixed << setprecision(3) << ans << endl;
    }
    return 0;
}

POJ3714 Raid 分治/K-D Tree的更多相关文章

  1. poj3714 Raid(分治求平面最近点对)

    题目链接:https://vjudge.net/problem/POJ-3714 题意:给定两个点集,求最短距离. 思路:在平面最近点对基础上加了个条件,我么不访用f做标记,集合1的f为1,集合2的f ...

  2. $Poj3714/AcWing\ Raid$ 分治/平面最近点对

    $AcWing$ $Sol$ 平面最近点对板子题,注意要求的是两种不同的点之间的距离. $Code$ #include<bits/stdc++.h> #define il inline # ...

  3. BZOJ.4182.Shopping(点分治/dsu on tree 树形依赖背包 多重背包 单调队列)

    BZOJ 题目的限制即:给定一棵树,只能任选一个连通块然后做背包,且每个点上的物品至少取一个.求花费为\(m\)时最大价值. 令\(f[i][j]\)表示在点\(i\),已用体积为\(j\)的最大价值 ...

  4. POJ3714 Raid

    Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10625   Accepted: 3192 Description ...

  5. POJ-3714 Raid 平面最近点对

    题目链接:http://poj.org/problem?id=3714 分治算法修改该为两个点集的情况就可以了,加一个标记... //STATUS:C++_AC_2094MS_4880KB #incl ...

  6. 洛谷P3806 点分治1 & POJ1741 Tree & CF161D Distance in Tree

    正解:点分治 解题报告: 传送门1! 传送门2! 传送门3! 点分治板子有点多,,,分开写题解的话就显得很空旷,不写又不太好毕竟初学还是要多写下题解便于理解 于是灵巧发挥压行选手习惯,开始压题解(bu ...

  7. 【点分治】bzoj1468 Tree

    同poj1741. 换了个更快的姿势,不会重复统计然后再减掉什么的啦~ #include<cstdio> #include<algorithm> #include<cst ...

  8. 【点分治】poj1741 Tree / poj2114 Boatherds / poj1987 Distance Statistics

    三道题都很类似.给出1741的代码 #include<cstdio> #include<algorithm> #include<cstring> using nam ...

  9. 第46届ICPC澳门站 K - Link-Cut Tree // 贪心 + 并查集 + DFS

    原题链接:K-Link-Cut Tree_第46屆ICPC 東亞洲區域賽(澳門)(正式賽) (nowcoder.com) 题意: 要求一个边权值总和最小的环,并从小到大输出边权值(2的次幂):若不存在 ...

随机推荐

  1. vue.js及项目实战[笔记]— 02 vue.js基础

    一. 基础 1. 注册全局组件 应用场景:多出使用的公共性能组件,就可以注册成全局组件,减少冗余代码 全局APIVue.component('组件名','组件对象') 2.附加功能:过滤器&监 ...

  2. 编程实践:使用java访问mySQL数据库

    1.虚拟机安装mySQL 服务器, 宿主机分别使用navicat工具和java代码 访问mySQL,组网图如下: 2. 查看mySQL的服务器状态,如下: 3. 服务器上查看数据库和数据表内容如下: ...

  3. jquery获取标签名,获取id

    var elementId = $(this).attr("id"); var tagName = $(this)[0].tagName;

  4. C# 异步编程1 APM 异步程序开发

    C#已有10多年历史,单从微软2年一版的更新进度来看活力异常旺盛,C#中的异步编程也经历了多个版本的演化,从今天起着手写一个系列博文,记录一下C#中的异步编程的发展历程.广告一下:喜欢我文章的朋友,请 ...

  5. python第九天----今天来晚了!

    作业 1. HAproxy配置文件操作1. 根据用户输入输出对应的backend下的server信息2. 可添加backend 和sever信息3. 可修改backend 和sever信息4. 可删除 ...

  6. 免费ARP

    1. 免费ARP基本概念 免费ARP,也叫Gratutious ARP.无故ARP.这种ARP不同于一般的ARP请求,它的Sender IP和Target IP字段是相同的,相当于是请求自己的IP地址 ...

  7. vue-cli在控制台创建vue项目时乱码的问题

    新装的win10系统,使用vue-cli在控制台创建项目时出现乱码,请问如何处理? 解决: 打开cmd,在控制台输入CHCP 65001,按回车键即可将编码格式设成utf-8,再创建就不会乱码了. 执 ...

  8. emacs org-mode文件转html文件

    Table of Contents 1. 发布站点 by emacs org-mode 1.1 org-mode 自带的导出方法 1.2 批量导出 1.3 css 美化 1.4 导出html 1. 发 ...

  9. Alpha冲刺!Day13 - 小结

    Alpha冲刺!Day13 - 小结 各个成员今日完成的任务 今天团队极限编程12小时,从早上九点要求每个人给出一张电脑全屏截图以示开始干活,每两小时汇报进度确认已经做了什么.现在在做什么. 各节点列 ...

  10. Windows安装pip方法

    1.下载pip 地址:https://pypi.python.org/pypi/pip#downloads 注意选择tar.gz压缩包,目前最新版本为9.0.1,这里选择的版本是:pip-9.0.1. ...