【CCF 2020 9】1-称检测点查询

问题描述
某市设有 个核酸检测点,编号从 到 ,其中 号检测点的位置可以表示为一个平面整数坐标

为方便预约核酸检测,请根据市民所在位置 ,查询距其最近的三个检测点。
多个检测点距离相同时,编号较小的视为更近。

输入格式
输入共 行。

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

第二行到第 行依次输入 个检测点的坐标。第 行()包含用空格分隔的两个整数

,表示 号检测点所在位置。

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

样例输入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 <iostream>
typedef struct hoslocation {
	int x, y;
}hos;
using namespace std;
int main()
{
	hos a[202];
	int n, x0, y0, i=1, min1=9000001, min2=9000001, min3=9000001,l1=0,l2=0,l3=0;
	cin >> n >> x0 >> y0;
	for (; i <= n; i++) {
		cin >> a->x >> a->y;
		if ((x0 - a->x) * (x0 - a->x) + (y0 - a->y) * (y0 - a->y)<min1) {
			if (min1 == min2&&i!=1) {
				if (l1 < l2) {
					l2 = l1;
					l1 = i;
				}
			}
			else {
				min2 = min1;
				min1 = (x0 - a->x) * (x0 - a->x) + (y0 - a->y) * (y0 - a->y);
				l2 = l1;
				l1 = i;
			}
		}
		else if ((x0 - a->x) * (x0 - a->x) + (y0 - a->y) * (y0 - a->y) >= min1 && (x0 - a->x) * (x0 - a->x) + (y0 - a->y) * (y0 - a->y) < min2) {
			if (min2 == min3 && i != 1 && i != 2 && i != 3) {
				if (l2 < l3) {
					l3 = l2;
					l2 = i;
				}
			}
			else {
				min3 = min2;
				min2 = (x0 - a->x) * (x0 - a->x) + (y0 - a->y) * (y0 - a->y);
				l3 = l2;
				l2 = i;
			}
		}
		else if ((x0 - a->x) * (x0 - a->x) + (y0 - a->y) * (y0 - a->y) >= min2 && (x0 - a->x) * (x0 - a->x) + (y0 - a->y) * (y0 - a->y) < min3) {
			min3 = (x0 - a->x) * (x0 - a->x) + (y0 - a->y) * (y0 - a->y);
			l3 = i;
		}
	}
	cout << l1 <<endl<< l2<<endl << l3;

	return 0;
}

错误在于当min1发生改变时,min2min3都要改变,我为考虑到此情况,且申请数组造成了较大的开销
下面是正确代码

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int n, c_x, c_y;
	scanf("%d%d%d", &n, &c_x, &c_y);
	int x, y;
	int a = 0x3f3f3f3f, b = 0x3f3f3f3e, c = 0x3f3f3f3d;
	int a_i = 0, b_i = 0, c_i = 0;
	for(int i=1; i<=n; i++)
	  {
	  	 scanf("%d%d", &x, &y);
	  	 int dis = pow((x - c_x) , 2) + pow((y - c_y) , 2);
	  	 if(dis < c)
	  	  {
	  	  	 a = b;
	  	  	 b = c;
	  	     c = dis;
			 a_i = b_i;
			 b_i = c_i;
			 c_i = i;	
		  }
		 else if(dis < b)
		 {
		 	a = b;
		 	b = dis;
		 	a_i = b_i;
		 	b_i = i;
		 }
		 else if(dis < a)
		 {
		 	a = dis;
		 	a_i = i;
		 }
	  }
	  printf("%d\n%d\n%d", c_i, b_i, a_i);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值