称检测点查询
原题链接
涉及的知识点:
- 优先队列
满分代码如下:
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
static bool cmp(pair<int, vector<int>>& m, pair<int, vector<int>>& n) {
if (m.first == n.first) {
return m.second[0] < n.second[0];
}
else {
return m.first < n.first;
}
}
int main()
{
int n, X, Y;
int x, y;
priority_queue<pair<int, vector<int>>, vector<pair<int, vector<int>>>, decltype(&cmp)> q(cmp);
scanf("%d%d%d", &n, &X, &Y);
for (int i = 0; i < n; ++i) {
scanf("%d%d", &x, &y);
int D = (x - X) * (x - X) + (y - Y) * (y - Y);
vector<int> temp;
temp.push_back(i);
temp.push_back(x);
temp.push_back(y);
if (q.size() < 3) {
q.emplace(D, temp);
}
else {
pair<int, vector<int>> t = q.top();
if (D < t.first || (D == t.first && i < t.second[0])) {
q.pop();
q.emplace(D, temp);
}
}
}
stack<int> st;
while (!q.empty()) {
pair<int, vector<int>> t = q.top();
q.pop();
st.emplace(t.second[0]);
}
while (!st.empty()) {
printf("%d\n", st.top() + 1);
st.pop();
}
return 0;
}