hdu-5992 Finding Hotels(kd-tree)

题目链接:

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('
');
}
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
",po[ansid].pos[1],po[ansid].pos[2],po[ansid].pri);
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/6116675.html