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. 学习笔记之Jira

    Jira | Issue & Project Tracking Software | Atlassian https://www.atlassian.com/software/jira The ...

  2. python-appium520-3引入unittest,编写自动化用例

    unittest是python的测试框架,和junit相似. test.py import unittest class Apptest(unittest.TestCase): def setUp(s ...

  3. kddcup2015

    kddcup2015,二分类,课程逃课预测.写了好久了,突然想起简单整理一下,以备后需. step1,预处理,利用numpy和pandas库,数值化特征,简单而优雅 #!/usr/bin/env py ...

  4. Solr Web增加Basic安全性验证

      Solr在5.0版本后,不再提供war包部署的方式,取而代之的是内置了jetty服务,   但是我们发现其中并没有内置任何安全性相关检查,任何人如果知道了我们的外网地址就能直接访问并修改其中的索引 ...

  5. laravel 5.3 ——路由(资源,别名)

    laravel的路由定义中,其中route:resoure(),可以直接定义类似restful风格的URL 例如:Route::resource('system/role','System\RoleC ...

  6. BTree,B-Tree,B+Tree,B*Tree的数据结构

    B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...

  7. 原型模式(ProtoType)

    用原型实例指定创建对象的种类,并且通过拷贝这些原型来创建新的对象. 原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节. .Net 在 system 命名空间中提供了I ...

  8. jenkins 构建一个maven项目

    1.首先在 全局工具配置 里配置maven的路径信息 这里因为之前已经下载了maven并放在了E盘,因此只需要在 MAVEN_HOME 添加maven文件夹的路径 如若本地还没maven,勾选 “自动 ...

  9. https://127.0.0.1:8080/test?param={%22..报错

    使用场景:spring boot 1.5.x,内置的tomcat版本为8.5.1 原因: tomcat自tomcat 8.0.35版本之后对URL参数做了比较规范的限制,必须遵循RFC 7230 an ...

  10. linux, Django安装MySQL-python==1.2.5报错

    先安装 sudo apt-get install libmysqlclient-dev