Codeforces 617C(Watering Flowers)

http://codeforces.com/contest/617/problem/C

                 Watering Flowers

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 test(s)
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


解题思路:
先将每个点与两个喷水处的距离求出来。然后对第一个喷水距离进行排序。
二重循环: 第一层是将排好序的from 1 to n
第二层是将 from i+1 to n 找到第二个喷水处的最大值

在期间更新ans.

我在这里犯得一个错误是 我习惯性的将 const long long max = 0x3f3f3f3f3f3f3f..自然WA了。。

以后有两种方法定义long long的上限:
1.加头文件 < climits > , LONG_LONG_MAX即为上限
2.const long long max = ~0ull>>1

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

const int N = 2000 + 500;
pair<long long,long long> p[N]; 

inline long long sq(long long x)  
{
    return x * x;
}

inline long long cal(long long x,long long y,long long x1,long long y1)
{
     return sq( (long long)(abs(x-x1)) ) + sq( (long long)(abs(y -y1)));
}
int main()
{
    int n;  cin>>n;
    long long x1,x2,y1,y2; cin>>x1>>y1>>x2>>y2;

    for(int i=1;i<=n;i++)
    {
        long long x,y;  cin>>x>>y;
        p[i] = make_pair(cal(x,y,x1,y1),-cal(x,y,x2,y2));
    }
    sort(p+1,p+1+n);
    long long ans = LONG_LONG_MAX; 
    for(int i=0;i<=n;i++)
    {
        long long now = 0;
        for(int j = i+1;j<=n;j++) now = max(now,-p[j].second);
        ans = min(ans,p[i].first + now);
    }
    cout<return 0;
}

你可能感兴趣的:(*Others)