In case of failure

http://acm.hdu.edu.cn/showproblem.php?pid=2966

Time Limit: 60000/30000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2843    Accepted Submission(s): 1209

Problem 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
 
Source

注意,最近点不能是自己,所以需要判cur.first!=0时才加入队列中

 #include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define N 100005
using namespace std; int n,m,id;//n是点数,m是维度,id是当前切的维度 struct sair{
long long p[];
bool operator<(const sair &b)const{
return p[id]<b.p[id];
}
}_data[N],data[N<<],tt[N];
int flag[N<<]; priority_queue<pair<long long,sair> >Q; void build(int l,int r,int rt,int dep){
if(l>r) return;
flag[rt]=;
flag[rt<<]=flag[rt<<|]=-;
id=dep%m;
int mid=l+r>>;
nth_element(_data+l,_data+mid,_data+r+);
data[rt]=_data[mid];
build(l,mid-,rt<<,dep+);
build(mid+,r,rt<<|,dep+);
} void query(sair p,int k,int rt,int dep){
if(flag[rt]==-) return;
pair<long long,sair> cur(,data[rt]);//获得当前节点
for(int i=;i<m;i++){//计算当前节点到P点的距离
cur.first+=(cur.second.p[i]-p.p[i])*(cur.second.p[i]-p.p[i]);
}
int idx=dep%m;
int fg=;
int x=rt<<;
int y=rt<<|;
if(p.p[idx]>=data[rt].p[idx]) swap(x,y);
if(~flag[x]) query(p,k,x,dep+);
//开始回溯
if(Q.size()<k){
if(cur.first!=){
Q.push(cur);
}
fg=;
}
else{
if(cur.first<Q.top().first){
if(cur.first!=){
Q.pop();
Q.push(cur);
}
}
if((p.p[idx]-data[rt].p[idx])*(p.p[idx]-data[rt].p[idx])<Q.top().first){
fg=;
}
}
if(~flag[y]&&fg){
query(p,k,y,dep+);
}
} long long ans; int main(){
int T;
scanf("%d",&T);
while(T--){
m=;
scanf("%d",&n);
for(int i=;i<=n;i++){
for(int j=;j<m;j++){
scanf("%lld",&_data[i].p[j]);
tt[i].p[j]=_data[i].p[j];
}
}
build(,n,,);
int k=;
for(int i=;i<=n;i++){
sair tmp;
for(int j=;j<m;j++)
tmp.p[j]=tt[i].p[j];
while(!Q.empty()){
Q.pop();
}
query(tmp,k,,);
ans=Q.top().first;
Q.pop();
printf("%lld\n",ans);
}
}
}

In case of failure的更多相关文章

  1. HDU #2966 In case of failure

    Overview 给出平面上两两不重合的\(n\)个整点, 求每个点到它在其他\(n-1\)个点的最近临点的欧几里得距离的平方. Solution k-d tree 模板题. 关于k-d tree, ...

  2. 【HDOJ】2966 In case of failure

    KD树,这东西其实在ML经常被使用,不过30s的时限还是第一次见. /* 2966 */ #include <iostream> #include <string> #incl ...

  3. hdu 2966 In case of failure k-d树

    题目链接 给n个点, 求出每个点到离它最近的点的距离. 直接建k-d树然后查询就可以  感觉十分神奇... 明白了算法原理但是感觉代码还不是很懂... #include <bits/stdc++ ...

  4. 【 HDU2966 】In case of failure(KD-Tree)

    BUPT2017 wintertraining(15) #5E HDU - 2966 题意 给平面直角坐标系下的n个点的坐标,求离每个点和它最近点的距离的平方.\(2 \le n \le 10^5\) ...

  5. HDU2966 In case of failure(浅谈k-d tree)

    嘟嘟嘟 题意:给定\(n\)个二维平面上的点\((x_i, y_i)\),求离每一个点最近的点得距离的平方.(\(n \leqslant 1e5\)) 这就是k-d tree入门题了. k-d tre ...

  6. kd树 hdu2966 In case of failure

    传送门:pid=2966" target="_blank">点击打开链接 题意:给n个点,求对于每一个点到近期点的欧几里德距离的平方. 思路:看鸟神博客学kd树劲啊 ...

  7. Scala For Java的一些参考

          变量 String yourPast = "Good Java Programmer"; val yourPast : String = "Good Java ...

  8. redis.conf配置详细解析

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...

  9. 基于ZooKeeper的分布式锁和队列

    在分布式系统中,往往需要一些分布式同步原语来做一些协同工作,上一篇文章介绍了Zookeeper的基本原理,本文介绍下基于Zookeeper的Lock和Queue的实现,主要代码都来自Zookeeper ...

随机推荐

  1. 【转载】深入浅出REST

    英文原文:A Brief Introduction to REST 作者:Stefan Tilkov ,译者:苑永凯,发布于 2007-12-25 不知你是否意识到,围绕着什么才是实现异构的应用到应用 ...

  2. selenium除错

    1.使用命令行启动selenium 下载selenium jar包 http://selenium-release.storage.googleapis.com/index.html 此处下载的是se ...

  3. CentOS下安装robot-framework

    http://www.cnblogs.com/hengwei/p/5464678.html http://www.2cto.com/kf/201405/305383.html https://pypi ...

  4. Windows下python 安装Mysqldb模块

    CMD执行 pip install mysql-python 报错如下: 1.如果报类似 Microsoft Visual C++ 9.0 is required < Unable to fin ...

  5. Centos 6.5 升级python到版本2.7.12

    查看python版本: python --version 1.下载Python-2.7.12 wget https://www.python.org/ftp/python/2.7.12/Python- ...

  6. 第11课 enum、sizeof、typedef 分析

    1. enum枚举类型 1.1 使用方法 (1)enum是C语言中的一种自定义类型 (2)enum值是可以根据需要自定义的的整型值 (3)第一个定义的enum值默认为0. (4)默认情况下的enum值 ...

  7. 【洛谷】P1641 [SCOI2010]生成字符串(思维+组合+逆元)

    题目 传送门:QWQ 分析 不想画图. https://www.luogu.org/problemnew/solution/P1641 好神仙的题啊. 代码 // luogu-judger-enabl ...

  8. django的小操作,查询效率up, 引用art-template模板+djangorestframework

    Part1: 提高查询效率newses = News.objects.select_related('category', 'author').get(id=1) # category和author字 ...

  9. 关于pthreads的使用

    产品想实现PHP端的多线程下载 百度了下找到了一个方法,通常需要开启PHP线程安全策略,就是 编译安装的时候  --enable-maintainer-zts 然后安装pthreads扩展, 但是pt ...

  10. linux7安装teamViewer

    参考网站:http://blog.sina.com.cn/s/blog_15308c8290102x72u.html 下载网站:https://www.teamviewer.com/zhCN/down ...