称检测点查询 CSP 202009-1

本文介绍了一个算法,用于根据市民当前位置查询附近三个最近的核酸检测点。输入包括检测点总数及市民位置坐标,输出则是距离市民最近的三个检测点的编号。

称检测点查询

问题描述

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

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

输入格式

输入共 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

~

就是用来vector加sort

代码

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
double GetDistance(int x1, int, int, int);
bool cmp(pair<int, double> p1, pair<int, double> p2)
{
    if(p1.second!=p2.second)
        return p1.second < p2.second;
    else 
        return p1.first<p2.first;
}
int main()
{
    int N;
    int x0, y0;
    cin >> N >> x0 >> y0;
    vector<pair<int, double> > v;
    int x, y;
    for (int i = 0; i < N; i++)
    {
        cin >> x >> y;
        v.push_back(make_pair(i, GetDistance(x0, y0, x, y)));
    }
    sort(v.begin(), v.end(), cmp);
    for (int i = 0; i < 3; i++)
        cout << v[i].first+1 << endl;
}

double GetDistance(int x1, int y1, int x2, int y2)
{
    return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值