CCF-CSP C/C++ 202009-1 称检测点查询 题解

CCF-CSP C/C++ 202009-1 称检测点查询

一、题目描述

1、背景

2020 年 6 月 8 日,国务院联防联控机制发布《关于加快推进新冠病毒核酸检测的实施意见》,提出对“密切接触者”等八类重点人群“应检尽检”,其他人群“愿检尽检”。

某市设有 n 个核酸检测点,编号从 1 到 n,其中 i 号检测点的位置可以表示为一个平面整数坐标 (xi,yi)。

为方便预约核酸检测,请根据市民所在位置 (X,Y),查询距其最近的三个检测点。

多个检测点距离相同时,编号较小的视为更近。

2、输入格式

输入共 n+1 行。

第一行包含用空格分隔的三个整数 n、X 和 Y,表示检测点总数和市民所在位置。

第二行到第 n+1 行依次输入 n 个检测点的坐标。第 i+1 行(1≤i≤n)包含用空格分隔的两个整数 xi 和 yi,表示 i 号检测点所在位置。

3、输出格式

输出共三行,按距离从近到远,依次输出距离该市民最近的三个检测点编号。

4、数据范围

全部的测试点满足,3≤n≤200,所有坐标均为整数且绝对值不超过 1000。

5、样例

输入样例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存到每个点的距离和序号,再改sort排序对距离排序。

**注意:**其中同等距离下记得加上序号小的要在前面

#include<iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

int cmp(pair<int,int>a,pair<int,int>b){
    
    
	if(a.first==b.first)return a.second<b.second;
	return a.first<b.first;
}
int main(){
    
    
	int n,x,y;
	int i;
	scanf("%d %d %d",&n,&x,&y);
	
	pair<int,int> maps[n+1];
	int x1,y1;
	int distance;
	
	for(i=1;i<=n;i++){
    
    
		scanf("%d %d",&x1,&y1);
		distance=(x1-x)*(x1-x)+(y1-y)*(y1-y);
		maps[i].first=distance;
		maps[i].second=i;
	}
	sort(maps+1,maps+1+n,cmp);
	for(i=1;i<=3;i++){
    
    
	
		printf("%d\n",maps[i].second);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/DonquixoteXXXXX/article/details/115420221