HDU5992-Finding Hotels

来源:互联网 发布:java如何获取微信code 编辑:程序博客网 时间:2024/05/15 12:45

Finding Hotels

                                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
                                                                                                 Total Submission(s): 988    Accepted Submission(s): 272


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
23 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 5
 

Sample Output
1 1 12 3 23 2 35 2 12 1 22 1 21 4 43 3 5
 

Source
2016ACM/ICPC亚洲区青岛站-重现赛(感谢中国石油大学)
 

Recommend
jiangzijing2015
 


题意:给出n个宾馆的坐标和价钱(每个宾馆的横坐标、纵坐标和价钱都不一样),现在有m个人,给出了m个人的坐标和最高能承受的价钱,现在问在这个交钱范围内最近的那个宾馆的坐标和价格,如果答案不止一个,那么就输出最先出现的那个
解题思路:kd-tree


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cctype>#include <map>#include <cmath>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst LL INF = 0x3f3f3f3f3f3f3f3f;const int N = 200000 + 5;const int demension = 2;//二维struct node{int pos[demension], c, id;int l, r;}a[N], x;int cmpDem;//以第cmpDem维作比较int ans,root;LL mi;bool cmp(const node &a, const node&b){return a.pos[cmpDem] < b.pos[cmpDem];}int build(int l, int r, int k){if (l > r) return 0;int mid = (l + r) / 2;//以第mid个元素为中心排序cmpDem = k;nth_element(a + l, a + mid, a + r + 1, cmp);//左右子树a[mid].l=build(l, mid - 1, (k + 1) % demension);a[mid].r=build(mid + 1, r, (k + 1) % demension);return mid;}void query(int k, int x, node p){if (!k) return;//op到根节点距离LL dis = 0;for (int i = 0; i < demension; i++) dis += 1LL * (p.pos[i] - a[k].pos[i])*(p.pos[i] - a[k].pos[i]);//更新ansif (a[k].c <= p.c){if (dis == mi&&a[k].id < a[ans].id) ans = k;if (dis < mi){mi = dis;ans = k;}}LL temp = 1LL * (p.pos[x] - a[k].pos[x])*(p.pos[x] - a[k].pos[x]);//到分裂平面距离if (p.pos[x] < a[k].pos[x]){query(a[k].l, (x + 1) % demension, p);if (mi >= temp) query(a[k].r, (x + 1) % demension, p);}else{query(a[k].r, (x + 1) % demension, p);if (mi >= temp) query(a[k].l, (x + 1) % demension, p);}}int main(){int t, n, q;scanf("%d", &t);while (t--){ scanf("%d%d", &n, &q);for (int i = 1; i <= n; i++){scanf("%d%d%d", &a[i].pos[0], &a[i].pos[1], &a[i].c);a[i].id = i, a[i].l = a[i].r = 0;}root=build(1, n, 0);while (q--){scanf("%d%d%d", &x.pos[0], &x.pos[1], &x.c);mi = INF;ans = -1;query(root, 0, x);printf("%d %d %d\n", a[ans].pos[0], a[ans].pos[1], a[ans].c);}}return 0;}

原创粉丝点击