题目背景
2020 年 6 月 8 日,国务院联防联控机制发布《关于加快推进新冠病毒核酸检测的实施意见》,提出对“密切接触者”等八类重点人群“应检尽检”,其他人群“愿检尽检”。
问题描述
某市设有n个核酸检测点,编号从1到n,其中i号检测点的位置可以表示为一个平面整数坐标 。
为方便预约核酸检测,请根据市民所在位置 ,查询距其最近的三个检测点。
多个检测点距离相同时,编号较小的视为更近。
输入格式
输入共n+1行。
第一行包含用空格分隔的三个整数 n、X和Y,表示检测点总数和市民所在位置。
第二行到第 n+1行依次输入n个检测点的坐标。第i+1行(1<=i<=n)包含用空格分隔的两个整数
和 xi,yi,表示 i号检测点所在位置。
输出格式
输出共三行,按距离从近到远,依次输出距离该市民最近的三个检测点编号。
样例输入1
3 2 2
2 2
2 3
2 4
样例输出1
1
2
3
样例输入2
5 0 1
-1 0
0 0
1 0
0 2
-1 2
样例输出2
2
4
1
这题这么迷??不到十分钟做出的题提交多次,全是编译出错,之前我用pair和map做CSP的其他题也没问题啊,这是什么操作??无奈改用结构体,对了。。。注意最后一行不输出换行,因为输出一共三行
编译出错
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(pair<int,int> p1,pair<int,int> p2){
if(p1.second!=p2.second)
return p1.second<p2.second;
else
return p1.first<p2.first;
}
int main(){
int n,x,y;
int a,b;
cin >> n >> x >> y;
map<int,int> m;
vector<pair<int,int> > v;
for(int i = 1;i<=n;i++){
cin >> a >> b;
int s = (a-x)*(a-x)+(b-y)*(b-y);
m[i] = s;
}
for(auto it = m.begin();it!=m.end();it++){
v.push_back(make_pair(it->first,it->second));
}
sort(v.begin(),v.end(),cmp);
cout << v[0].first;
for(int i = 1;i<3;i++)
cout << endl << v[i].first;
return 0;
}
AC版
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct{
int id;
int s;
}Test;
bool cmp(Test t1,Test t2){
if(t1.s!=t2.s)
return t1.s<t2.s;
else
return t1.id<t2.id;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n,x,y;
int a,b;
vector<Test> v;
Test t;
cin >> n >> x >> y;
for(int i = 1;i<=n;i++){
cin >> a >> b;
t.id = i;
t.s = (a-x)*(a-x)+(b-y)*(b-y);
v.push_back(t);
}
sort(v.begin(),v.end(),cmp);
cout << v[0].id;
for(int i = 1;i<3;i++)
cout << endl << v[i].id;
return 0;
}