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.
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.
Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.
2 -1 0 5 3 0 2 5 2
6
4 0 0 5 0 9 4 8 3 -1 0 1 4
33
The first sample is (r12 = 5, r22 = 1): The second sample is (r12 = 1, r22 = 32):
题目大意:
给你n个点,和两个圆心的位子,让你用两个圆覆盖住所有的点,使得r1*r1+r2*r2最小。
思路:
1、首先我们做出每个点到圆心1的距离记为dis1,和道圆心2的距离记为dis2。
2、然后我们按照dis1降序排序,排序后,如果我们选择第i个点去用圆1来覆盖,那么其实从i+1-n个点都可以用圆1来覆盖了(因为dis1是降序的),那么从第1个点到第i-1个点就需要用圆2来覆盖,那么我们这个时候我们只需要维护一下从第1个点到第i-1个点的dis2最大值即可,那么当前枚举出来的情况的解就是a【i】.dis1+maxn(这个维护的最大值)
3、按照dis1降序排序之后,我们再按照dis2降序排序,按照上述方式同理维护一个最优解即可。注意数据范围比较大,数据类型和初始化要注意一下。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define ll __int64
struct node
{
ll dis1,dis2;
}a[12121];
ll dis(ll x,ll y,ll x2,ll y2)
{
ll tmp1=(x-x2)*(x-x2)+(y-y2)*(y-y2);
return tmp1;
}
int cmp(node a,node b)
{
if(a.dis2==b.dis2)return a.dis1>b.dis1;
else return a.dis2>b.dis2;
}
int cmp2(node a,node b)
{
if(a.dis1==b.dis1)return a.dis2>b.dis2;
else return a.dis1>b.dis1;
}
int main()
{
int n;
ll x1,y1,x2,y2;
while(~scanf("%d%I64d%I64d%I64d%I64d",&n,&x1,&y1,&x2,&y2))
{
for(int i=0;i<n;i++)
{
ll x,y;
scanf("%I64d%I64d",&x,&y);
a[i].dis1=dis(x,y,x1,y1);
a[i].dis2=dis(x,y,x2,y2);
}
sort(a,a+n,cmp);
ll ans=100000000000000000;
for(int i=0;i<n;i++)
{
ll tmpmaxn=0;
for(int j=0;j<i;j++)
{
tmpmaxn=max(tmpmaxn,a[j].dis1);
}
ans=min(ans,tmpmaxn+a[i].dis2);
}
sort(a,a+n,cmp2);
for(int i=0;i<n;i++)
{
ll tmpmaxn=0;
for(int j=0;j<i;j++)
{
tmpmaxn=max(tmpmaxn,a[j].dis2);
}
ans=min(ans,tmpmaxn+a[i].dis1);
}
printf("%I64d\n",ans);
}
}