思路
大概想到两种,一种是优先队列,重构优先级,第二种是贪心,选到比原来小的就更新,方便起见我用的是第二种,代码如下:
满分代码实现(自解)
#include <map>
#include <set>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
void solve();
};
void Solution::solve() {
int n = -1;
double X = -1, Y = -1;
cin >> n >> X >> Y;
double x = -1, y = -1;
double distance = -1;
int f = 1, s = 2, t = 3;
double first = 0, second = 0, third = 0;
cin >> x >> y;
first = (X - x) * (X - x) + (Y - y) * (Y - y);
cin >> x >> y;
second = (X - x) * (X - x) + (Y - y) * (Y - y);
if (second < first) {
swap(f, s);
swap(first, second);
}
cin >> x >> y;
third = (X - x) * (X - x) + (Y - y) * (Y - y);
if (third < first) {
swap(s, t);
swap(f, s);
swap(third, second);
swap(first, second);
}
else if (third < second) {
swap(s, t);
swap(third, second);
}
for (int i = 4; i <= n; i++) {
cin >> x >> y;
distance = (X - x) * (X - x) + (Y - y) * (Y - y);
if (distance < first) {
third = second;
second = first;
t = s;
s = f;
f = i;
first = distance;
}
else if (distance < second) {
third = second;
t = s;
s = i;
second = distance;
}
else if (distance < third) {
t = i;
third = distance;
}
}
cout << f << endl;
cout << s << endl;
cout << t;
}
int main() {
// 提高cin,cout的速度
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
Solution s;
s.solve();
return 0;
}