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. Kong管理UI -kong-dashboard (附kong封装webservice方法)

    本文仍然是在centos 6.7的环境下进行                 本文转载请注明出处 —— xiaoEight btw如果要正常使用管理UI,前提为kong已经正常run(可参考)起来,此 ...

  2. Linux下的压缩(zip)解压(unzip)缩命令

      .zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip命令 unzip -o ...

  3. 被折腾的sql编程

  4. 由一条普通的link引用引发的无数问号,大家能回答的帮忙回答回答吧.

    <link type="text/css" rel="stylesheet" href="1.css" /> 对于前台工作者来说 ...

  5. OPatch failed with error code 73(OracleHomeInventory gets null oracleHomeInfo)

    OPatch failed with error code 73(OracleHomeInventory gets null oracleHomeInfo) 1.问题描述 [oracle@dou_ra ...

  6. android 系统架构简介

    Android系统采取的是分层的架构,根据官方文档提供的架构图,我们将android的系统架构分成5层,如图: 1.Application Framework (应用框架) application f ...

  7. Python - Django - 登录页面

    登录页 login.html: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta char ...

  8. python实现列表页数据的批量抓取练手练手的

    python实现列表页数据的批量抓取,练手的,下回带分页的 #!/usr/bin/env python # coding=utf-8 import requests from bs4 import B ...

  9. rhel7配置tiger vnc详解 centos6配置安装vnc-server

    参考网站:http://blog.51cto.com/xjsunjie/1963463     结合  https://blog.csdn.net/wamath/article/details/760 ...

  10. EventUtil——跨浏览器的事件对象

    . 首页 博客园 联系我 前言:什么是EventUtil?. EventUtil对象全见. addHandler方法. removeHandler方法. event对象与getEvent方法. tar ...