题目链接:

Finding Hotels

Time Limit: 2000/1000 MS (Java/Others)    

Memory Limit: 102400/102400 K (Java/Others)

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
 
题意:
 
给出n个宾馆的坐标和价钱,现在有m个人,给出了m个人的坐标和最高能承受的价钱,现在问在这个交钱范围内最近的那个宾馆的坐标和价格;
如果答案不止一个,那么就输出最先出现的那个;
 
思路:
 
这是青岛现场赛的一道题,但时没做出来,止步银,用kd-tree,一开始我用方差的那个确定划分的维度,一直T,后来变成了按二叉树的深度交替变换维度和加了输入挂才过了;
感觉这题常数卡的好紧;
 
AC代码:
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn=2e5+20;
const LL inf=1e18; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}
int n,m,now,ansid;
LL ansdis,minp[maxn];
struct node
{
LL pos[3],pri;
int id;
}po[maxn],op;
int cmp(node a,node b){return a.pos[now]<b.pos[now];}
void build(int L,int R,int dep,int fa)
{
if(L>R)return ;
int mid=(L+R)>>1;
now=dep;
nth_element(po+L,po+mid,po+R+1,cmp);
minp[mid]=po[mid].pri;
build(L,mid-1,3-dep,mid);build(mid+1,R,3-dep,mid);
minp[fa]=min(minp[fa],minp[mid]);
}
inline LL get_dis(LL tep){return tep*tep;}
void query(int L,int R,int dep)
{
if(L>R)return ;
int mid=(L+R)>>1;
if(minp[mid]>op.pri)return ;
LL dis=get_dis(po[mid].pos[1]-op.pos[1])+get_dis(po[mid].pos[2]-op.pos[2]);
if(op.pri>=po[mid].pri)
{
if(dis<ansdis)ansdis=dis,ansid=mid;
else if(dis==ansdis&&po[mid].id<po[ansid].id)ansid=mid;
}
LL tep=get_dis(po[mid].pos[dep]-op.pos[dep]);
if(op.pos[dep]<=po[mid].pos[dep])
{
query(L,mid-1,3-dep);
if(tep<=ansdis)query(mid+1,R,3-dep);
}
else
{
query(mid+1,R,3-dep);
if(tep<=ansdis)query(L,mid-1,3-dep);
}
}
int main()
{
int T;
read(T);
while(T--)
{
read(n);read(m);
for(int i=1;i<=n;i++)
{
po[i].id=i;
for(int j=1;j<=2;j++)read(po[i].pos[j]);
read(po[i].pri);
}
build(1,n,1,0);
while(m--)
{
ansdis=inf;
read(op.pos[1]);read(op.pos[2]);read(op.pri);
query(1,n,1);
printf("%lld %lld %lld\n",po[ansid].pos[1],po[ansid].pos[2],po[ansid].pri);
}
}
return 0;
}

  

hdu-5992 Finding Hotels(kd-tree)的更多相关文章

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

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

  2. Finding Hotels

    Finding Hotels http://acm.hdu.edu.cn/showproblem.php?pid=5992 Time Limit: 2000/1000 MS (Java/Others) ...

  3. HDU5992 - Finding Hotels

    原题链接 Description 给出个二维平面上的点,每个点有权值.次询问,求所有权值小于等于的点中,距离坐标的欧几里得距离最小的点.如果有多个满足条件的点,输出最靠前的一个. Solution 拿 ...

  4. AOJ DSL_2_C Range Search (kD Tree)

    Range Search (kD Tree) The range search problem consists of a set of attributed records S to determi ...

  5. k-d tree 学习笔记

    以下是一些奇怪的链接有兴趣的可以看看: https://blog.sengxian.com/algorithms/k-dimensional-tree http://zgjkt.blog.uoj.ac ...

  6. 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree

    2648: SJY摆棋子 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2459  Solved: 834[Submit][Status][Discu ...

  7. K-D Tree

    这篇随笔是对Wikipedia上k-d tree词条的摘录, 我认为解释得相当生动详细, 是一篇不可多得的好文. Overview A \(k\)-d tree (short for \(k\)-di ...

  8. K-D Tree题目泛做(CXJ第二轮)

    题目1: BZOJ 2716 题目大意:给出N个二维平面上的点,M个操作,分为插入一个新点和询问到一个点最近点的Manhatan距离是多少. 算法讨论: K-D Tree 裸题,有插入操作. #inc ...

  9. k-d Tree in TripAdvisor

    Today, TripAdvisor held a tech talk in Columbia University. The topic is about k-d Tree implemented ...

随机推荐

  1. EC笔记,第二部分:7.为多态基类声明虚析构函数

    7.为多态基类声明虚析构函数 1.为多态基类声明虚析构函数 code1: class A{ public: int* a; A():a(new int(5)) {} ~A(){ delete a; } ...

  2. JVM垃圾回收(GC)原理

    一.基本垃圾回收算法 1.引用计数(Reference Counting) 比较古老的回收算法.原理是此对象有一个引用则增加一个引用计数,删除一个引用则较少一个引用计数.垃圾回收时,只回收引用计数为0 ...

  3. 【夯实PHP系列】购物车代码说明PHP的匿名函数

    1. 定义:匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数.最经常用作回调函数(callback)参数的值.当然,也有其它应 ...

  4. PHP 小数点保留两位

    最近在做统计这一块内容,接触关于数字的数据比较多, 用到了三个函数来是 数字保留小数后 N 位: 接下来简单的介绍一下三个函数: 1.number_format echo number_format( ...

  5. java web学习总结(十三) -------------------使用Session防止表单重复提交

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...

  6. C#中 反射中的Assembly(装载程序集):

    反射中的Assembly(装载程序集):可以通过Assembly的信息来获取程序的类,实例等编程需要用到的信息.  String assemblyName = @"NamespaceRef& ...

  7. android 史上最简单易懂的跨进程通讯(Messenger)!

    不需要AIDL也不需要复杂的ContentProvider,也不需要SharedPreferences或者共享存储文件! 只需要简单易懂的Messenger,它也称为信使,通过它可以在不同进程中传递m ...

  8. 【代码笔记】iOS-下拉选项cell

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> //加入头文件 #import "ComboBo ...

  9. iOS之触摸及手势

    触摸事件 iOS中的事件: 在用户使用app过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型:    view的触摸事件处理: 响应者对象: 在iOS中不是任何对象都能处理事件,只有继承了 ...

  10. SVN 提交代码时提示文件已经存在解决办法

    在SVN里面找到这个文件,把这个文件右键delete删除掉,然后提交一下commit ,然后在项目中也把这个文件删除了,然后再添加到项目中提交,commit一下,就好了,解决