HDU5992Finding Hotels 【K-D tree】

来源:互联网 发布:js页面获取当前时间 编辑:程序博客网 时间:2024/05/21 10:23
Finding HotelsTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 473    Accepted Submission(s): 122Problem DescriptionThere 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.InputThe 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.OutputFor 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 Input23 31 1 13 2 32 3 22 2 12 2 22 2 35 51 4 42 1 24 5 35 2 13 3 53 3 13 3 23 3 33 3 43 3 5Sample Output1 1 12 3 23 2 35 2 12 1 22 1 21 4 43 3 5Source2016ACM/ICPC亚洲区青岛站-重现赛(感谢中国石油大学)

基本算是裸的kd树了
直接建树 直接查找 加个比较价格就可以了

#include<iostream>#include<cstdlib>#include<cstdio>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<ctime>#include <string.h>#include<math.h>using namespace std;#define ll long long#define pii pair<int,int>const ll inf=1e17;const int N = 200000 + 5;const int M = 20000 + 5;const int demension=2;//二维struct P{    int pos[demension],c,id;}hotel[N];P kdtree[N];double var[demension];//方差int split[N];//i为根的子树 分裂方式为第split[i]维int cmpDem;//以第cmpDem维作比较bool cmp(const P&a,const P&b){    return a.pos[cmpDem]<b.pos[cmpDem];}void build(int l,int r){    if(l<r){        int mid=(l+r)/2;        //计算每一维方差        for(int i=0;i<demension;++i){            double ave=0;//均值            for(int j=l;j<=r;++j){                ave+=hotel[j].pos[i];            }            ave/=(r-l+1);            var[i]=0;//方差            for(int j=l;j<=r;++j){                var[i]+=(hotel[j].pos[i]-ave)*(hotel[j].pos[i]-ave);            }            var[i]/=(r-l+1);        }        //更新mid为树根时 分裂方法为第几维        split[mid]=-1;        double maxVar=-1;        for(int i=0;i<demension;++i){//找方差最大的维            if(var[i]>maxVar){                maxVar=var[i];                split[mid]=i;            }        }        //以第mid个元素为中心 排序        cmpDem=split[mid];        nth_element(hotel+l,hotel+mid,hotel+r+1,cmp);        //左右子树        build(l,mid-1);        build(mid+1,r);    }}int ansIndex;ll ansDis;//ansDis=欧几里得距离^2void query(int l,int r,P op){    if(l>r){        return;    }    int mid=(l+r)/2;    //op到根节点距离    ll dis=0;    for(int i=0;i<demension;++i){        dis+=(ll)(op.pos[i]-hotel[mid].pos[i])*(op.pos[i]-hotel[mid].pos[i]);    }    //更新ans    if(hotel[mid].c<=op.c){        if(dis==ansDis&&hotel[mid].id<hotel[ansIndex].id){            ansIndex=mid;        }        if(dis<ansDis){            ansDis=dis;            ansIndex=mid;        }    }    int d=split[mid];    ll radius=(ll)(op.pos[d]-hotel[mid].pos[d])*(op.pos[d]-hotel[mid].pos[d]);//到分裂平面距离    if(op.pos[d]<hotel[mid].pos[d]){        query(l,mid-1,op);        if(ansDis>=radius){            query(mid+1,r,op);        }    }    else{        query(mid+1,r,op);        if(ansDis>=radius){            query(l,mid-1,op);        }    }}int main(){    //freopen("/home/lu/Documents/r.txt","r",stdin);    int T;    scanf("%d",&T);    while(T--){        int n,m;        scanf("%d%d",&n,&m);        for(int i=0;i<n;++i){            scanf("%d%d%d",&hotel[i].pos[0],&hotel[i].pos[1],&hotel[i].c);            hotel[i].id=i;        }        build(0,n-1);        P p;        for(int i=0;i<m;++i){            scanf("%d%d%d",&p.pos[0],&p.pos[1],&p.c);            ansDis=inf;            ansIndex=-1;            query(0,n-1,p);            printf("%d %d %d\n",hotel[ansIndex].pos[0],hotel[ansIndex].pos[1],hotel[ansIndex].c);        }    }    return 0;}
0 0