HDU5992-Finding Hotels

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

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 long
const 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]);
	//更新ans
	if (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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值