HDU5992Finding Hotels 【K-D tree】
2017-02-23 19:29
260 查看
Finding Hotels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 473 Accepted Submission(s): 122 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 2016ACM/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 ; P kdtree ; double var[demension];//方差 int split ;//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=欧几里得距离^2 void 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; }
相关文章推荐
- SIFT中的K-D Tree的研究(转载)
- [HDOJ 4812][Vjudge 48318] D Tree [树的分治]
- HDU 4812 D Tree 树分区+逆+hash新位置
- BZOJ 2626: JZPFAR|K-D tree
- CodeForces - 675D Tree Construction (set&数据结构)
- k-d tree背诵用模板
- [bzoj1941]k-d tree 模板
- K-D Tree 算法详解及Python实现
- k-d tree的优化查找算法BBF 【转】
- 【HDU 4812】D Tree【树的分治】
- k-d tree 介绍
- 1941: [Sdoi2010]Hide and Seek|K-D Tree
- k-d Tree及其Java实现
- [BZOJ2850]巧克力王国(K-D tree)
- [Ipsc2015]Generating Synergy K-D tree
- K-D Tree数据结构介绍
- HDU 4812 D Tree 树的点分治
- HDU 4812 D Tree (树上点分治)
- 2850: 巧克力王国 K-D tree