问题描述
某市设有 个核酸检测点,编号从 到 ,其中 号检测点的位置可以表示为一个平面整数坐标
。
为方便预约核酸检测,请根据市民所在位置 ,查询距其最近的三个检测点。
多个检测点距离相同时,编号较小的视为更近。
输入格式
输入共 行。
第一行包含用空格分隔的三个整数 、 和 ,表示检测点总数和市民所在位置。
第二行到第 行依次输入 个检测点的坐标。第 行()包含用空格分隔的两个整数
和
,表示 号检测点所在位置。
输出格式
输出共三行,按距离从近到远,依次输出距离该市民最近的三个检测点编号。
样例输入1
3 2 2
2 2
2 3
2 4
Data
样例输出1
1
2
3
Data
样例输入2
5 0 1
-1 0
0 0
1 0
0 2
-1 2
Data
样例输出2
2
4
1
#include<bits/stdc++.h>
using namespace std;
struct point
{
int length;
int num;
};
//最小堆的实现
struct cmp
{
bool operator()(point a,point b)
{
if(a.length!=b.length)
return a.length>b.length;
else
{
return a.num>b.num;
}
}
};
int main()
{
priority_queue<point,vector<point>,cmp> q;
int n;cin>>n;
int x,y; cin>>x>>y;
int x1,y1;
for(int i=0;i<n;i++)
{
cin>>x1>>y1;
long long len=(x1-x)*(x1-x)+(y1-y)*(y1-y);
point x;x.length=len;x.num=i+1;
q.push(x);
}
int cnt=0;
while(cnt<3)
{
cout<<q.top().num<<endl;
q.pop();
cnt++;
}
// system("pause");
}
本文介绍了一种算法,用于解决市民在给定检测点坐标集合中寻找与其位置最近的三个检测点的问题。通过输入市民坐标,程序利用优先队列实现高效搜索并输出编号。
7190

被折叠的 条评论
为什么被折叠?



