Raid

Raid

https://ac.nowcoder.com/acm/problem/108310

时间限制: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;
}
牛客算法竞赛入门课 文章被收录于专栏

给雨巨打call

全部评论
分治的时候第41行为什么先判断k是否一致会出错呀
点赞 回复 分享
发布于 2021-01-30 16:24

相关推荐

神哥了不得:你简历字体有点不太协调呀,下面的字实在太小了呀,而且项目也不太行,建议换几个高质量的项目,面试会多很多
点赞 评论 收藏
分享
菜鸡29号:根据已有信息能初步得出以下几点: 1、硕士排了大本和大专 2、要求会多语言要么是招人很挑剔要么就是干的活杂 3、给出校招薪资范围过于巨大,说明里面的薪资制度(包括涨薪)可能有大坑
点赞 评论 收藏
分享
评论
6
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务