Description
A flowerbed has many flowers and two fountains.
You can adjust the water pressure and set any values r1(r1 ≥ 0) and r2(r2 ≥ 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 and r2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceed r1, or the distance to the second fountain doesn't exceed r2. It's OK if some flowers are watered by both fountains.
You need to decrease the amount of water you need, that is set such r1 and r2 that all the flowers are watered and the r12 + r22 is minimum possible. Find this minimum value.
Input
The first line of the input contains integers n, x1, y1, x2, y2 (1 ≤ n ≤ 2000, - 107 ≤ x1, y1, x2, y2 ≤ 107) — the number of flowers, the coordinates of the first and the second fountain.
Next follow n lines. The i-th of these lines contains integers xi and yi ( - 107 ≤ xi, yi ≤ 107) — the coordinates of the i-th flower.
It is guaranteed that all n + 2 points in the input are distinct.
Output
Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.
Sample Input
2 -1 0 5 3 0 2 5 2
6
4 0 0 5 0 9 4 8 3 -1 0 1 4
33
Hint
The first sample is (r12 = 5, r22 = 1): The second sample is (r12 = 1, r22 = 32):
题意:有两个喷泉,给你n朵花的坐标和喷泉的坐标,然后你可以自由设置喷泉的半径,问你在确保每朵花都能被浇到的情况下,两喷泉半径平方的最小值是多少。
思路:一开始以为肯定是个贪心什么的,结果试了很多的贪心策略都不对,仔细一想,确实存在问题。比赛结束后,没事的时候就会想想这个题,于是便想到了直接遍历,题目给了2s,突然我灵光来了,想到了一个算法~想出来之后发现代码是很简单的~
1.先按照到一个喷泉的距离从大到小排序,然后想一想,花必定会被浇,不是被1就是被2,那么我们枚举一个喷泉喷到的花,剩下的自然是第二个喷泉喷到的了。所以我们要排序,排序之后完全就不用考虑下面的,默认都是被这个喷泉浇了的了,理由嘛就是贪心,这样做,绝对的是最优的(读者自己仔细考虑考虑)。这样复杂度就很低了。
2.注意,因为排序好的喷泉从最大点开始枚举,至少是有一个点的,少了全是喷泉2浇的情况!
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN=2000+5;
const double eps=1e-5;
struct node
{
long long l1,l2;
//按照到第一个喷泉的距离的平方从大到小排序
bool operator < (const node a)const
{
return a.l1<l1;
}
}l[MAXN];
int main()
{
long long n,x1,y1,x2,y2,x,y;
int i,j;
scanf("%lld%lld%lld%lld%lld",&n,&x1,&y1,&x2,&y2);
for(i=0;i<n;++i)
{
scanf("%lld%lld",&x,&y);
l[i].l1=(x-x1)*(x-x1)+(y-y1)*(y-y1);
l[i].l2=(x-x2)*(x-x2)+(y-y2)*(y-y2);
}
sort(l,l+n);
l[n].l1=0;//为枚举全是喷泉2浇的做特殊处理
long long ans=LONG_LONG_MAX;
for(i=0;i<=n;++i)
{
long long mm=0;
for(j=0;j<i;++j)//剩下的自然是上面的点了
{
mm=max(mm,l[j].l2);
}
ans=min(ans,mm+l[i].l1);
}
printf("%lld\n",ans);
return 0;
}