题目链接

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

Problem Description
There are N hotels all over the world. Each hotel has a location and a price. M guests want to find a hotel with an acceptable price and a minimum distance from their locations. The distances are measured in Euclidean metric.
 
Input
The first line is the number of test cases. For each test case, the first line contains two integers N (N ≤ 200000) and M (M ≤ 20000). Each of the following N lines describes a hotel with 3 integers x (1 ≤ x ≤ N), y (1 ≤ y ≤ N) and c (1 ≤ c ≤ N), in which x and y are the coordinates of the hotel, c is its price. It is guaranteed that each of the N hotels has distinct x, distinct y, and distinct c. Then each of the following M lines describes the query of a guest with 3 integers x (1 ≤ x ≤ N), y (1 ≤ y ≤ N) and c (1 ≤ c ≤ N), in which x and y are the coordinates of the guest, c is the maximum acceptable price of the guest.
 
Output
For each guests query, output the hotel that the price is acceptable and is nearest to the guests location. If there are multiple hotels with acceptable prices and minimum distances, output the first one.
 
Sample Input
2
3 3
1 1 1
3 2 3
2 3 2
2 2 1
2 2 2
2 2 3
5 5
1 4 4
2 1 2
4 5 3
5 2 1
3 3 5
3 3 1
3 3 2
3 3 3
3 3 4
3 3 5
 
Sample Output
1 1 1
2 3 2
3 2 3
 
5 2 1
2 1 2
2 1 2
1 4 4
3 3 5
 
Source
 
 
题意:有n个旅店(x,y,c),(x,y,c)表示旅店的平面坐标和住宿价格,现在有m个游客(x,y,c)想住宿,(x,y,c)表示游客的平面坐标和最高接受的价格,现在要求输出每一位游客选择的旅店(价格在小于游客接受的最高价格下的最近的旅店,如果有多个旅店符合要求,输出输入时排在前面的旅店);
 
思路:K-D树,我在参加青岛现场赛时根本不知道这个算法,所以没做出来,最后铜牌了,有点可惜。这道题不难,是一道K-D树的模板题(如果不知道K-D树,想了解的话,看苟神的博客)。K-D树其实说到底是一棵二叉搜索树,但普通的二叉搜索树的节点是一维的,可以比较大小的向下搜索到叶子节点找到解,但K-D树的每个节点是多维的,在每个节点处有多维,这多维分量共同对结果产生影响,那么可以采用计算方差或者每一个分量轮流着来进行划分(我看网上大部分人都觉得使用每一个分量轮流来划分方法比较好,我以这种方法为例叙说),这样就不能绝对保证另一边的子树一定不包含解,但包含解的可能性要小于这边的子树,所以求解时优先搜索这边的子树,在回溯时判断另一边的子树是否需要搜索。从整体上看呢,感觉K-D树是一种暴力搜索+剪枝。
唉,自己还是太弱,加油!
 
代码如下:(另外这题,我写完用C++提交超时了,看了一下本题排名,发现所有过的代码都是G++提交的,我试着G++提交,过了。我不太明白这两种方式有什么不同,可有大神能说一下^_^)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <bitset>
using namespace std;
#define Sqrt2(x) (x)*(x)
typedef long long LL;
int N,M,idx; struct Node
{
int f[];
int id;
bool operator<(const Node& s)const
{
return f[idx]<s.f[idx];
}
}data[],tr[*];
int flag[*];
pair<LL,Node> ans; void build(int l,int r,int i,int deep)
{
if(l>r) return;
flag[i]=;
flag[i<<]=; flag[i<<|]=;
idx=deep%;
int mid=(l+r)>>;
nth_element(data+l,data+mid,data+r+);
tr[i]=data[mid];
build(l,mid-,i<<,deep+);
build(mid+,r,i<<|,deep+);
} void query(Node p,int i,int deep)
{
if(!flag[i]) return ;
pair<LL,Node> c;
c.second=tr[i];
c.first=(LL)(Sqrt2((LL)p.f[]-tr[i].f[])+Sqrt2((LL)p.f[]-tr[i].f[]));
bool fg=;
int idm=deep%;
int x=i<<;
int y=i<<|;
if(p.f[idm]>=tr[i].f[idm]) swap(x,y);
if(flag[x]) query(p,x,deep+);
if(ans.first==-){
if(c.second.f[]<=p.f[])
ans.first=c.first,ans.second=c.second;
fg=;
}
else {
if(c.second.f[]<=p.f[]&&(c.first<ans.first||(c.first==ans.first&&c.second.id<ans.second.id)))
ans.first=c.first,ans.second=c.second;
if((LL)(Sqrt2(tr[i].f[idm]-p.f[idm]))<ans.first)
fg=;
}
if(fg&&flag[y]) query(p,y,deep+);
} int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++)
{
for(int j=;j<;j++)
scanf("%d",&data[i].f[j]);
data[i].id=i;
}
build(,N,,);
while(M--)
{
Node p;
for(int i=;i<;i++)
scanf("%d",&p.f[i]);
ans.first=-;
query(p,,);
printf("%d %d %d\n",ans.second.f[],ans.second.f[],ans.second.f[]);
}
}
return ;
}

2016 ICPC青岛站---k题 Finding Hotels(K-D树)的更多相关文章

  1. 2016 ICPC大连站---F题 Detachment

    题意:输入一个x,将x拆分成一些小的数(这些数不能相同,即x=a1+a2+......   ai!=aj when i!=j),然后这些数相乘得到一个成积(s=a1*a2*......),求最大的乘积 ...

  2. HDU 5992 Finding Hotels(KD树)题解

    题意:n家旅店,每个旅店都有坐标x,y,每晚价钱z,m个客人,坐标x,y,钱c,问你每个客人最近且能住进去(非花最少钱)的旅店,一样近的选排名靠前的. 思路:KD树模板题 代码: #include&l ...

  3. 2016 ACM-ICPC 青岛站网络赛G题 题解

    [参考博客][https://blog.csdn.net/Tawn0000/article/details/82255682] 题意: 将n个数按照每k个一组来合并,合并需要花费的cost是两个数的长 ...

  4. ICPC青岛站网络赛-C-高效模拟

    嗯这道辣鸡题,当时我队友写了错误的代码,我稍微改动了,思路基本上是对了,但是就是超时,我第一直觉是我这个算法思路是没有任何问题的,但是就是TLE,我感觉这个算法已经优化的不能再优化了啊...后面就怀疑 ...

  5. HDU 5881 Tea -2016 ICPC 青岛赛区网络赛

    题目链接 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不大于1. 你无法测量壶里剩下水的 ...

  6. HDU 5879 Cure -2016 ICPC 青岛赛区网络赛

    题目链接 题意:给定一个数n,求1到n中的每一项的平方分之一的累加和. 题解:题目没有给数据范围,而实际上n很大很大超过long long.因为题目只要求输出五位小数,我们发现当数大到一定程度时值是固 ...

  7. HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

    题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...

  8. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  9. HDU 5884 Sort -2016 ICPC 青岛赛区网络赛

    题目链接 #include <iostream> #include <math.h> #include <stdio.h> #include<algorith ...

随机推荐

  1. 记录一次bug解决过程:数据迁移

    一 总结 不擅长语言表达,勤于沟通,多锻炼 调试MyBatis中SQL语法:foreach 问题:缺少关键字VALUES.很遗憾:它的错误报的让人找不着北. 二 BUG描述:MyBatis中批量插入数 ...

  2. Zabbix 漏洞分析

    之前看到Zabbix 出现SQL注入漏洞,自己来尝试分析. PS:我没找到3.0.3版本的 Zabbix ,暂用的是zabbix 2.2.0版本,如果有问题,请大牛指点. 0x00 Zabbix简介 ...

  3. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  4. github免输用户名/密码SSH登录的配置

    从github上获取的,自己整理了下,以备后用. Generating an SSH key mac windows SSH keys are a way to identify trusted co ...

  5. TFS 2015 敏捷开发实践 – 在Kanban上运行一个Sprint

    前言:在 上一篇 TFS2015敏捷开发实践 中,我们给大家介绍了TFS2015中看板的基本使用和功能,这一篇中我们来看一个具体的场景,如何使用看板来运行一个sprint.Sprint是Scrum对迭 ...

  6. SpringMVC_简单小结

    SpringMVC是一个简单的.优秀的框架.应了那句话简单就是美,而且他强大不失灵活,性能也很优秀. 机制:spring mvc的入口是servlet,而struts2是filter(这里要指出,fi ...

  7. Linux.NET实战手记—自己动手改泥鳅(下)

    在上回合中,我们不痛不痒的把小泥鳅的数据库从只能供在Windows下运行的Access数据库改为支持跨平台的MYSQL数据库,毫无营养的修改,本回合中,我们将把我们修改后得来的项目往Linux中部署. ...

  8. 舍弃Nunit拥抱Xunit

    前言 今天与同事在讨论.Net下测试框架的时候,说到NUnit等大多数测试框架的SetUp以及TearDown方法并不是显得那么完美,所以在公司内部的项目中采用了Xunit框架.那么究竟是什么样的原因 ...

  9. MapReduce剖析笔记之六:TaskTracker初始化任务并启动JVM过程

    在上面一节我们分析了JobTracker调用JobQueueTaskScheduler进行任务分配,JobQueueTaskScheduler又调用JobInProgress按照一定顺序查找任务的流程 ...

  10. HTML5 & CSS3初学者指南(1) – 编写第一行代码

    介绍 网络时代已经到来.现在对人们来说,每天上网冲浪已经成为一种最为常见的行为. 在网页浏览器中输入一段文本地址,就像http://www.codeproject.com,等待一下,网页就加载到浏览器 ...