CodeForces 617C Watering Flowers

C - Watering Flowers
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  CodeForces 617C

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 nx1y1x2y2 (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

Input
2 -1 0 5 3
0 2
5 2
Output
6
Input
4 0 0 5 0
9 4
8 3
-1 0
1 4
Output
33

Hint

The first sample is (r12 = 5r22 = 1):  The second sample is (r12 = 1r22 = 32): 

FAQ | About Virtual Judge |  Forum |  Discuss |  Open Source Project

题意:给定两个圆的圆心和n个点,找到两个圆的半径r1和r2覆盖所有点。问最小的r1^2 + r2^2。


思路:预处理距离,对于一个点来说,要么被圆1覆盖,要么被圆2覆盖,那么枚举下就好了。时间复杂度O(n^2)。不过是可以优化的,先求出距离d2需要的信息,然后扫一遍d1,时间复杂度O(nlogn)。

 
    





O(n^2)枚举每一个花朵在第一个喷泉的距离,第一个喷泉距离比它小的 就不用管了。第一个喷泉距离比它大的,那就一定在第二个喷泉范围里面,求最大值。

枚举第一个距离的每一种情况,求最小值。(其中有可能每一个花朵都在第二个喷泉里,这种情况要考虑到。)

代码:

[cpp]  view plain  copy
 
 
  1. #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<cctype>
    #include <map>
    #define max(a,b)(a>b?a:b)
    #define min(a,b)(a<b?a:b)
    #define INF 0x3f3f3f3f
    typedef long long ll;


    using namespace std;


    struct node
    {
        ll d1,d2;
    }p[2100];


    ll dis(ll x,ll a,ll y,ll b)
    {
        return (a-x)*(a-x)+(b-y)*(b-y);
    }


    int main()
    {
        int n,i,j;
        ll ans,x1,y1,x2,y2,x,y;
        while(scanf("%d%lld%lld%lld%lld",&n,&x1,&y1,&x2,&y2)!=EOF)
        {
            for(i=1;i<=n;i++)
            {
                 scanf("%lld%lld",&x,&y);
                 p[i].d1=dis(x,x1,y,y1);
                 p[i].d2=dis(x,x2,y,y2);
            }


            ll Min=1*1e18;


            for(i=1;i<=n+1;i++)
            {
                ll m2=0;
                for(j=1;j<=n;j++)
                {
                    if(p[j].d1>p[i].d1)
                       m2=max(m2,p[j].d2);
                }
                Min=min(Min,m2+p[i].d1);
            }
            printf("%lld\n",Min);
        }
        return 0;
    }

你可能感兴趣的:(CodeForces 617C Watering Flowers)