时间限制:C/C++ 5秒,其他语言10秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union's attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N nuclear power stations and breaking down any of them would disable the system.

The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?

输入描述:

The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 100000).
The next N lines describe the positions of the stations. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station.
The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent.  

输出描述:

For each test case output the minimum distance with precision of three decimal placed in a separate line.

示例1

输入

复制
2
4
0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3
4
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

输出

复制
1.414
0.000

分治

先对整体数据放在一个数组中进行排序,按照x第一关键字升序,y第二关键字升序。
那么之后再考虑分治的做法,其实这道题就是分治的入门题,板子题。
先对给定的区间,左边求一下最小值,右边求一下最小值,那么还可能的取值就是起点落在左区间终点落在右区间,再去跑一下左右区间,记得特殊的位置一定不可能更新答案的时候即使break出来。

#pragma GCC target("avx,sse2,sse3,sse4,popcnt")
#pragma GCC optimize("O2,O3,Ofast,inline,unroll-all-loops,-ffast-math")
#include<cstdio>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include<cmath>
#include<cstring>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
const ll MOD = 1e9 + 7;
inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar())    s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void write(ll x) { if (!x) { putchar('0'); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-');  int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]); }
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1;   while (b) { if (b & 1)  ans *= a;       b >>= 1;      a *= a; }   return ans; }   ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
inline int lowbit(int x) { return x & (-x); }

const int N = 1e5 + 7;
const int INF = 0x3f3f3f3f;
struct Node {
    int x, y;
    int k;
    bool operator < (const Node& b) const {
        if (x != b.x)   return x < b.x;
        return y < b.y;
    }
}a[N << 1];

double dis(int x, int y) {
    return sqrt(1.0 * (a[x].x - a[y].x) * (a[x].x - a[y].x) + 1.0 * (a[x].y - a[y].y) * (a[x].y - a[y].y));
}

double solve(int l, int r) {
    if (l == r) return INF;
    int mid = l + r >> 1;
    double ans = INF;
    ans = min(ans, min(solve(l, mid), solve(mid + 1, r)));
    for (int i = mid; i >= l; --i) {
        if (a[mid].x - a[i].x > ans) break;
        for (int j = mid; j <= r; ++j) {
            if (a[j].x - a[i].x > ans)   break;
            if (a[j].k != a[i].k)   ans = min(ans, dis(i, j));
        }
    }
    return ans;
}

int main() {
    int T = read();
    while (T--) {
        int n = read();
        for (int i = 1; i <= n; ++i)
            a[i].x = read(), a[i].y = read(), a[i].k = 0;
        for (int i = n + 1; i <= 2 * n; ++i)
            a[i].x = read(), a[i].y = read(), a[i].k = 1;
        sort(a + 1, a + 1 + 2 * n);
        printf("%.3f\n", solve(1, 2 * n));
    }
    return 0;
}