博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[HDU 2966]In case of failure
阅读量:5157 次
发布时间:2019-06-13

本文共 3178 字,大约阅读时间需要 10 分钟。

Description

To help their clients deal with faulty Cash Machines, the board of The Planar Bank has decided to stick a label expressing sincere regret and sorrow of the bank about the failure on every ATM. The very same label would gently ask the customer to calmly head to the nearest Machine (that should hopefully
work fine).
In order to do so, a list of two-dimensional locations of all n ATMs has been prepared, and your task is to find for each of them the one closest with respect to the Euclidean distance.

Input

The input contains several test cases. The very first line contains the number of cases t (t <= 15) that follow. Each test cases begin with the number of Cash Machines n (2 <= n <= 10^5). Each of the next n lines contain the coordinates of one Cash Machine x,y (0 <= x,y <=10^9) separated by a space. No two
points in one test case will coincide.

Output

For each test case output n lines. i-th of them should contain the squared distance between the i-th ATM from the input and its nearest neighbour.

Sample Input

2 10 17 41 0 34 24 19 8 28 14 12 45 5 27 31 41 11 42 45 36 27 15 0 0 1 2 2 3 3 2 4 0 8 4 7 4 6 3 6 1 8 0 11 0 12 2 13 1 14 2 15 0

Sample Output

200 100 149 100 149 52 97 52 360 97 5 2 2 2 5 1 1 2 4 5 5 2 2 2 5

题目大意

求平面最近点对。

题解

裸的$KD-tree$。

我们用线性结构来存储这颗树,其主要思想是将一段区间内的元素中间那个元素作为根节点,其余作为左右子树,用个决策函数来划分。

询问的时候就比较暴力...其主要就是通过根节点以及其与子树的关系来优化搜索过程。

1 //It is made by Awson on 2017.11.25 2 #include  3 #include 
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #define LL long long16 #define LD long double17 #define Max(a, b) ((a) > (b) ? (a) : (b))18 #define Min(a, b) ((a) < (b) ? (a) : (b))19 #define dist(a, b, c, d) (sqr(a-c)+sqr(b-d))20 #define sqr(x) ((x)*(x))21 #define y1 yy22 #define count COUNT23 using namespace std;24 const int N = 1e5;25 26 int n, now;27 LL ans;28 struct tt {29 LL x[2];30 bool operator < (const tt &b) const {31 return x[now] < b.x[now];32 }33 }a[N+5], b[N+5];34 struct KD_tree {35 void build(int l, int r, int flag) {36 if (l >= r) return;37 now = flag;38 int mid = (l+r)>>1;39 nth_element(a+l, a+mid, a+r+1);40 build(l, mid-1, flag^1); build(mid+1, r, flag^1);41 }42 void query(int l, int r, int flag, tt p) {43 if (l > r) return;44 int mid = (l+r)>>1;45 LL tmp = dist(a[mid].x[0], a[mid].x[1], p.x[0], p.x[1]);46 if (tmp && tmp < ans) ans = tmp;47 if (a[mid].x[flag] < p.x[flag]) {48 query(mid+1, r, flag^1, p);49 if (ans > sqr(a[mid].x[flag]-p.x[flag])) 50 query(l, mid-1, flag^1, p);51 }else {52 query(l, mid-1, flag^1, p);53 if (ans > sqr(a[mid].x[flag]-p.x[flag])) 54 query(mid+1, r, flag^1, p);55 }56 }57 }kd;58 59 void work() {60 scanf("%d", &n);61 for (int i = 1; i <= n; i++) {62 scanf("%lld%lld", &a[i].x[0], &a[i].x[1]);63 b[i] = a[i];64 }65 kd.build(1, n, 0);66 for (int i = 1; i <= n; i++) {67 ans = 1e18;68 kd.query(1, n, 0, b[i]);69 printf("%lld\n", ans);70 }71 }72 int main() {73 int t; cin >> t;74 while (t--) work();75 return 0;76 }

 

转载于:https://www.cnblogs.com/NaVi-Awson/p/7895069.html

你可能感兴趣的文章
php 基础入门篇之前言
查看>>
spark 源码分析之八--Spark RPC剖析之TransportContext和TransportClientFactory剖析
查看>>
TCP/IP及内核参数优化调优(转)
查看>>
01.MVC5安装Ext.Net
查看>>
Python中的lambda表达式
查看>>
现代软件工程—构建之法---第三章:练习与讨论
查看>>
react项目 npm run eject报错
查看>>
IOS数据本地存储的四种方式--
查看>>
QT笔记之VS2010 Qt中导入qrc资源文件
查看>>
java二维码小试牛刀
查看>>
Java的内存机制
查看>>
多任务的同步与相互排斥
查看>>
Linux Unix shell 编程指南学习笔记(第五部分)
查看>>
Safari new Date()
查看>>
小程序支付前端代码
查看>>
ffmpeg 编译成功,Mark一下
查看>>
redhat 6.8 配置 centos6 163 的 yum 源
查看>>
CentOS 6.5 Nginx 配置
查看>>
编写一个Java项目,定义包,在包下定义包含main方法的类,在main方法中声明8种基本数据类型的变量并赋值,练习数据类型转换。...
查看>>
linux --- 部署前后端分离项目
查看>>