[HDU 2966]In case of failure

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 <map>
 3 #include <set>
 4 #include <cmath>
 5 #include <ctime>
 6 #include <queue>
 7 #include <stack>
 8 #include <cstdio>
 9 #include <string>
10 #include <vector>
11 #include <cstdlib>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 #define LL long long
16 #define LD long double
17 #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 yy
22 #define count COUNT
23 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 }
时间: 2024-07-31 15:30:06

[HDU 2966]In case of failure的相关文章

hdu 2966 In case of failure(KD-tree)

题目链接:hdu 2966 In case of failure 题意: 给你n个点,让你输出每个点到最近点的欧式距离. 题解: KD-树裸题,板子抄的鸟神的. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=(a);i<=(b);++i) 3 using namespace std; 4 using ll=long long; 5 6 namespace KD_Tree{ 7 const int N=1e5+7,DI=2;

hdu 2966 In case of failure kdtree模板题

问求每个点距离平方的最小的点 kd-tree模板题…… 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(x) cerr<<#x<<"=="<<(x)<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int>

HDU ACM 2966 In case of failure -&gt;K_D树模版题

分析:k_d树的模版题,参考了别人的写的:划分的时候采用坐标跨度作为分割依据的效率略比采用树的深度作为划分依据的高:nth_element函数比sort函数的效率高:全部采用getchar和putchar效率也能提高一些. #include<iostream> #include<algorithm> using namespace std; struct POINT { int x,y; }; struct K_D_Node { POINT mid; //分割中点 int spli

HDU 2966 Aragorn&#39;s Story 树链剖分第一题 基础题

Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges c

php使用redis

在 http://www.redis.net.cn/ 能找到所有关于redis的信息,包括安装.命令.在编程语言中的使用等等.这里就不讲如何安装redis了,因为在上面的网站中都能找到.下面直接讲redis是如何在php中使用的. 一.phpredis扩展 安装phpredis扩展前需要下载phpredis扩展,下载前根据你本地php环境选择对应的phpreids扩展(选不对的话,就有可能无法使用redis了),建议使用php5.4.x版本环境. 1.php执行phpinfo()函数,根据下面截

redis应用之安装配置介绍

一.redis介绍: 1.redis定义: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主持.redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set --有序集合)和hash(哈希类型).这些数据类型都

FFmpeg源代码简单分析:avformat_alloc_output_context2()

本文简单分析FFmpeg中常用的一个函数:avformat_alloc_output_context2().在基于FFmpeg的视音频编码器程序中,该函数通常是第一个调用的函数(除了组件注册函数av_register_all()).avformat_alloc_output_context2()函数可以初始化一个用于输出的AVFormatContext结构体.它的声明位于libavformat\avformat.h,如下所示. /** * Allocate an AVFormatContext

Swift网络请求(Moya篇)

在使用Alamofire进行网络请求的时候,相信大部分的同学都会封装一个抽象的NetworkLayer,如"APIManager" 或者 "NetworkModel"等等.但是位置业务功能增加,会渐渐混合各种请求,不够清晰,而Moya能很好地解决这类问题.Moya在Alamofire基础上进行封装,是一个允许高度自定义的网络层,可以根据具体的需求进行接口的设置.具体的介绍可以参考Moya的官方链接,结构图如下: 接下来就介绍一下Moya的一些常见的用法: (一)根据

Swift中的错误处理

前言 任何代码都会发生错误,这些错误有些是可以补救的,有些则只能让程序崩溃.良好的错误处理能够让你的代码健壮性提高,提高程序的稳定性. 本文的Swift版本:Swift 3 Objective C 返回nil 如果出错了,就返回空是Objective C中的一种常见的处理方式.因为在Objective C中,向nil发送消息是安全的.比如: - (instancetype)init { self = [super init]; if (self) { } //如果初始化失败,会返回nil ret