描述
每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了。因为有太多太多好吃的地方可以去吃,而小L又比较懒不想走太远,所以小L会先找到距离他最近的M家餐馆然后再做筛选。
小L现在所在的位置和每家餐馆的位置用同一笛卡尔坐标系中的点表示,而点与点之间的距离为欧几里得距离,对于点p = (p1, p2,…, pn)和点q = (q1,q2,…, qn),两者的距离定义如下
现给出在K维空间中小L所处的位置的坐标以及n个餐馆的位置,请帮助小L完成他的需求。
输入
第1行包含两个整数n和K,1≤n≤5000,1≤K≤5。
接下来n行,每行包含K个数,表示每个餐馆的坐标。
接下来1行,包含一个数t,1≤t≤10000,表示小L询问的数目。
每次询问包括两行。第1行包含K个数,表示小L所在的坐标。第2行包含一个数M,1≤M≤10。
所有坐标值不会超过10000。
输入数据包含多组数据,请逐个处理直到文件结束。
输出
对于每一个询问,输出m+1行:
第1行输出:”the closest M points are:”,其中M在输入中给出。
接下来M行输出距离最近的M家餐馆的坐标,按照由近及远的顺序输出。
输出数据保证答案唯一。保证从小L的位置到最近的M+1家餐馆位置各不相同,这说明如下输入数据:
2 2
1 1
3 3
1
2 2
1
不会存在。
样例输入
3 2
1 1
1 3
3 4
2
2 3
2
2 3
1
样例输出
the closest 2 points are:
1 3
3 4
the closest 1 points are:
1 3
#include <bits/stdc++.h>
using namespace std;
int ss(int x){
return x*x;
}
int cpt(vector<int> &x,vector<int> &y)
{
int res=0,n=x.size();
for(int i=0;i<n;++i){
res+=ss(x[i]-y[i]);
}
return res;
}
int main()
{
// freopen("3.txt","r",stdin);
int n,k;
while(~scanf("%d %d",&n,&k)){
int y[8]={};
vector<vector<int>> v;
for(int i=0;i<n;++i){
for(int j=0;j<k;++j){
scanf("%d",y+j);
}
v.push_back(vector<int>(y,y+k));
}
int t,m;
scanf("%d",&t);
while(t--){
vector<int> vy(k);
for(int j=0;j<k;++j){
scanf("%d",&vy[j]);
}
scanf("%d",&m);
typedef pair<int,int> ele;
vector<ele> A;
for(int i=0;i<n;++i){
A.push_back(ele(cpt(v[i],vy),i));
}
partial_sort(A.begin(),A.begin()+m,A.end());
printf("the closest %d points are:\n",m);
for(int i=0;i<m;++i){
for(int x:v[A[i].second]){
printf("%d ",x);
}
printf("\n");
}
}
}
return 0;
}