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
- HDU5992Finding Hotels 【K-D tree】
- HDU5992 Finding Hotels(Kd-tree)
- k-d tree
- k-d tree算法
- K-D tree 数据结构
- k-d tree算法
- K-d Tree算法
- k-d tree算法
- k-d tree算法
- k-d tree算法
- k-d tree算法
- k-d tree算法
- k-d tree算法
- k-d tree算法
- k-d tree算法
- k-d tree算法
- K-D Tree
- k-d tree算法
- 页面主体高度不固定,如何让页面的footer始终在最底部
- eclipse创建maven
- 16、SQL Server: if else和case
- java 使用javassist 动态修改注解的值
- 实践干货!猿题库 iOS 客户端架构设计
- HDU5992Finding Hotels 【K-D tree】
- 从汇编角度来解析c程序的运行过程
- Android M运行时权限
- post的两种方式区别:form-data和x-www-form-urlencoded
- mybatis实战教程(mybatis in action),mybatis入门到精通
- 二叉排序树(建树)
- opencv学习_8 ( 图像细化)
- Struts2详述
- HTML