您的位置:首页 > 编程语言 > C语言/C++

Coderforces 617C Watering Flowers 【暴力】

2018-02-04 17:08 295 查看

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.

Example

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

Note

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



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



题意:

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

思路:

预处理距离,对于一个点来说,要么被圆1覆盖,要么被圆2覆盖,由于数据不大,那么直接暴力枚举下就好了。

ac代码:

#include <stdio.h>
typedef long long ll;
const ll INF  = 0x3f3f3f3f;
const int maxn = 2e3 + 5;
ll a[maxn], b[maxn], c[maxn], d[maxn];
//a存x坐标,b存y坐标,c存到喷泉1的距离平方,dc存到喷泉2的距离平方
ll f(ll x1, ll x, ll y1, ll y) {
return (x1-x)*(x1-x) + (y1-y)*(y1-y);
}
int main()//注意 ll
{
ll n, x1, x2, y1, y2;
ll result;
ll min = INF << 32; //注意int最大值和long long最大值差32位
scanf("%lld %lld %lld %lld %lld", &n, &x1, &y1, &x2, &y2);
for (int i = 0; i < n; ++i)
{
scanf("%lld %lld", &a[i], &b[i]);
c[i] = f(a[i], x1, b[i], y1);//枚举每一个花到喷泉1的距离
d[i] = f(a[i], x2, b[i], y2);//枚举每一个花到喷泉2的距离
}
ll max = 0;
for (int i = 0; i < n; ++i)//第i个在喷泉1里面,不在1的在喷泉2里面
{
max = 0;
for (int j = 0; j < n; ++j)
{
if(c[j] > c[i])
{
if(d[j] > max)
max = d[j];
}
}
result = max + c[i];
if(result < min)
min = result;
}
for (int i = 0; i < n; ++i)//第i个在喷泉2里面,不在2的在喷泉1里面
{
max = 0;
for (int j = 0; j < n; ++j)
{
if(d[j] > d[i])
{
if(c[j] > max)
max = c[j];
}
}
result = max + d[i];
if(result < min)
min = result;
}
printf("%lld\n", min);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息