Finding Hotels

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

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 2180    Accepted Submission(s): 688

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

结构体内用友元函数这题会T....

模板题

 #include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define N 200005
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.second.p[]<=p.p[]){
Q.push(cur);
}
fg=;
}
else{
if(cur.first<=Q.top().first&&cur.second.p[]<=p.p[]){
if(cur.first==Q.top().first){
if(cur.second.p[]<Q.top().second.p[]){
Q.pop();
Q.push(cur);
}
}
else{
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+);
}
} sair ans; int main(){
int T;
scanf("%d",&T);
int k;
while(T--){
scanf("%d %d",&n,&k);
m=;
for(int i=;i<=n;i++){
scanf("%lld %lld %lld",&_data[i].p[],&_data[i].p[],&_data[i].p[]);
_data[i].p[]=i;
}
build(,n,,);
sair tmp;
for(int i=;i<=k;i++){
while(!Q.empty()){
Q.pop();
}
scanf("%lld %lld %lld",&tmp.p[],&tmp.p[],&tmp.p[]);
tmp.p[]=0x3f3f3f3f;
query(tmp,,,);
ans=Q.top().second;
Q.pop();
printf("%lld %lld %lld\n",ans.p[],ans.p[],ans.p[]);
}
}
}

Finding Hotels的更多相关文章

  1. hdu-5992 Finding Hotels(kd-tree)

    题目链接: Finding Hotels Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 102400/102400 K (Java/ ...

  2. HDU5992 - Finding Hotels

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

  3. 2016 ICPC青岛站---k题 Finding Hotels(K-D树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over ...

  4. HDU 5992/nowcoder 207K - Finding Hotels - [KDTree]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5992 题目链接:https://www.nowcoder.com/acm/contest/207/K ...

  5. 【22.95%】【hdu 5992】Finding Hotels

    Problem Description There are N hotels all over the world. Each hotel has a location and a price. M ...

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

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

  7. 【HDU5992】Finding Hotels 【KD树】

    题意 给出n个酒店的坐标和价格,然后m个查询,每个查询给出一个人的坐标和能承受的最大价格,然后找出在他价格承受范围以内,距离他最近的宾馆,如果有多个,那么输出第一个 分析 kd树的模板题 #inclu ...

  8. 【kd-tree】hdu5992 Finding Hotels

    比较裸的kd-tree,但是比较考验剪枝. 貌似除了经典的矩形距离剪枝之外, 还必须加个剪枝是某个矩形内的最小价格如果大于价格限制的话,则剪枝. #include<cstdio> #inc ...

  9. Hdu-5992 2016ACM/ICPC亚洲区青岛站 K.Finding Hotels KDtree

    题面 题意:二维平面上有很多点,每个点有个权值,现在给你一个点(很多组),权值v,让你找到权值小于等于v的点中离这个点最近的,相同的输出id小的 题解:很裸的KDtree,但是查询的时候有2个小限制, ...

随机推荐

  1. [UE4]蓝图重构

    假设现在有一个蓝图类BP_GunRife(已经有其它很多类在使用这个类),现在要增加另外一把枪BP_BunLauncher. 可以新建一个父类BP_Gun,让BP_GunRife和BP_BunLaun ...

  2. 补充2:Golang 一些特性

    Go语言的这些地方都做的还不错: 拥有自动垃圾回收: 不用手动释放内存 一个包系统: Go 语言的源码复用建立在包(package)基础之上.包通过 package, import, GOPATH 操 ...

  3. Koa 框架整理

    学习交流 Koa使用了ES6规范的generator和异步编程是一个更轻量级Web开发的框架,Koa 的先天优势在于 generator.由于是我个人的分享交流,所以Node基础.ES6标准.Web开 ...

  4. 自己写的jQuery放大镜插件效果(二)(采用只有一张图片的思路)

    废话不多说,先看效果图,和上一章节用的是同一个小图片: 这个方法实现的放大有个弊端就是放大倍数设置的过大的话,会带来图片上的模糊.但是图片加载的话要比使用2张图片加载的快很多 插件代码: ; (fun ...

  5. GameObject数组

    一.声明 GameObject[] rock = ]; 二.遍历 可以先声明一个GameObject的数组GameObject[], 然后把需要遍历的对象放进去,然后可以用下面两种方式遍历:1.for ...

  6. 【Python编程:从入门到实践】chapter8 函数

    chapter8 函数 8.6 将函数存储在模块中 8.6.1 导入整个模块 要让函数是可导入的,的先创建模块.模块 的扩展名为.py的文件 import pizza 8.6.2 到导入特定的函数 f ...

  7. python装饰器(docorator)详解

    引言: 装饰器是python面向对象编程三大器之一,另外两个迭代器.生成器只是我现在还没有遇到必须使用的场景,等确实需要用到的时候,在补充资料:装饰器在某些场景真的是必要的,比如定义了一个类或者一个函 ...

  8. create a bootable USB stick on Ubuntu

    https://tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu?_ga=2.141187314.17572770 ...

  9. ldap复制

    1.使用yum命令安装openldap,openldap-servers,openldap-clients $ yum install openldap $ yum install openldap- ...

  10. python入门-函数(二)

    1 函数传递参数 def greet_users(names): """向列表中的每个用户都发处问候""" for name in name ...