Finding Hotels
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 1755 Accepted Submission(s): 546
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
题意:
直角坐标系上有n个宾馆,每个宾馆的价格都不同,有m个人想住宾馆,已知他们的位置和最大可以接受的价格,对于每个人求出最近的能住的宾馆在哪儿
思路:
将价格也作为一维,上个三维KD树模板搞定(青岛现场赛时暴力可以过,当时我们就是暴力过得)
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
#define deep 3
LL ans;
int loc, id;
typedef struct Point
{
int id;
LL x[3];
bool operator < (const Point &b) const
{
if(x[loc]<b.x[loc])
return 1;
return 0;
}
}Point;
Point p[200005], temp[200005], Aim;
void Create(int l, int r, int now)
{
int m;
if(l>=r)
return;
m = (l+r)/2;
loc = now%deep;
nth_element(p+l, p+m, p+r+1);
Create(l, m-1, now+1);
Create(m+1, r, now+1);
}
void Jud(Point now)
{
LL bet;
if(now.x[2]>Aim.x[2])
return;
bet = (now.x[1]-Aim.x[1])*(now.x[1]-Aim.x[1])+(now.x[0]-Aim.x[0])*(now.x[0]-Aim.x[0]);
if(bet<ans || bet==ans && now.id<id)
ans = bet, id = now.id;
}
void Find(int l, int r, int now)
{
int m;
LL len;
loc = now%deep;
m = (l+r)/2;
if(l>=r)
{
if(l==r)
Jud(p[r]);
return;
}
Jud(p[m]);
if(loc!=2)
{
len = p[m].x[loc]-Aim.x[loc];
if(len>=0)
{
Find(l, m-1, now+1);
if(len*len<ans)
Find(m+1, r, now+1);
}
else
{
Find(m+1, r, now+1);
if(len*len<ans)
Find(l, m-1, now+1);
}
}
else
{
len = p[m].x[loc]-Aim.x[loc];
if(len>=0)
Find(l, m-1, now+1);
else
{
Find(m+1, r, now+1);
Find(l, m-1, now+1);
}
}
}
int main(void)
{
int T, n, Q, i, j;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &Q);
for(i=1;i<=n;i++)
{
for(j=0;j<=deep-1;j++)
scanf("%lld", &p[i].x[j]);
p[i].id = i;
temp[i] = p[i];
}
Create(1, n, 0);
while(Q--)
{
ans = 1e18, id = -1;
for(j=0;j<=deep-1;j++)
scanf("%lld", &Aim.x[j]);
Find(1, n, 0);
printf("%lld", temp[id].x[0]);
for(j=1;j<=deep-1;j++)
printf(" %lld", temp[id].x[j]);
puts("");
}
}
return 0;
}