Finding Hotels

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....

模板题

  1 #include<iostream>
  2 #include<queue>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<cstdio>
  6 #define N 200005
  7 using namespace std;
  8 
  9 int n,m,id;//n是点数,m是维度,id是当前切的维度
 10 
 11 struct sair{
 12     long long p[5];
 13     bool operator<(const sair &b)const{
 14         return p[id]<b.p[id];
 15     }
 16 }_data[N],data[N<<3],tt[N];
 17 int flag[N<<3];
 18 
 19 priority_queue<pair<long long,sair> >Q;
 20 
 21 void build(int l,int r,int rt,int dep){
 22     if(l>r) return;
 23     flag[rt]=1;
 24     flag[rt<<1]=flag[rt<<1|1]=-1;
 25     id=dep%m;
 26     int mid=l+r>>1;
 27     nth_element(_data+l,_data+mid,_data+r+1);
 28     data[rt]=_data[mid];
 29     build(l,mid-1,rt<<1,dep+1);
 30     build(mid+1,r,rt<<1|1,dep+1);
 31 }
 32 
 33 void query(sair p,int k,int rt,int dep){
 34     if(flag[rt]==-1) return;
 35     pair<long long,sair> cur(0,data[rt]);//获得当前节点
 36     for(int i=0;i<m;i++){//计算当前节点到P点的距离
 37         cur.first+=(cur.second.p[i]-p.p[i])*(cur.second.p[i]-p.p[i]);
 38     }
 39     int idx=dep%m;
 40     int fg=0;
 41     int x=rt<<1;
 42     int y=rt<<1|1;
 43     if(p.p[idx]>=data[rt].p[idx]) swap(x,y);
 44     if(~flag[x]) query(p,k,x,dep+1);
 45     //开始回溯
 46     if(Q.size()<k){
 47         if(cur.second.p[2]<=p.p[2]){
 48             Q.push(cur);
 49         }
 50         fg=1;
 51     }
 52     else{
 53         if(cur.first<=Q.top().first&&cur.second.p[2]<=p.p[2]){
 54             if(cur.first==Q.top().first){
 55                 if(cur.second.p[3]<Q.top().second.p[3]){
 56                     Q.pop();
 57                     Q.push(cur);
 58                 }
 59             }
 60             else{
 61                 Q.pop();
 62                 Q.push(cur);
 63             }
 64         }
 65         if(((p.p[idx]-data[rt].p[idx])*(p.p[idx]-data[rt].p[idx]))<Q.top().first){
 66             fg=1;
 67         }
 68     }
 69     if(~flag[y]&&fg){
 70         query(p,k,y,dep+1);
 71     }
 72 }
 73 
 74 sair ans;
 75 
 76 
 77 int main(){
 78     int T;
 79     scanf("%d",&T);
 80     int k;
 81     while(T--){
 82         scanf("%d %d",&n,&k);
 83         m=2;
 84         for(int i=1;i<=n;i++){
 85             scanf("%lld %lld %lld",&_data[i].p[0],&_data[i].p[1],&_data[i].p[2]);
 86             _data[i].p[3]=i;
 87         }
 88         build(1,n,1,0);
 89         sair tmp;
 90         for(int i=1;i<=k;i++){
 91             while(!Q.empty()){
 92                 Q.pop();
 93             }
 94             scanf("%lld %lld %lld",&tmp.p[0],&tmp.p[1],&tmp.p[2]);
 95             tmp.p[3]=0x3f3f3f3f;
 96             query(tmp,1,1,0);
 97             ans=Q.top().second;
 98             Q.pop();
 99             printf("%lld %lld %lld\n",ans.p[0],ans.p[1],ans.p[2]);
100         }
101     }
102 }
View Code

猜你喜欢

转载自www.cnblogs.com/Fighting-sh/p/9876357.html