Coderforces 617C Watering Flowers 【暴力】

C. Watering Flowers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 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
Note

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



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


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


AC代码:注意ans 初始化的细节,坑死了。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (2000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
struct Node{
    LL d1, d2;
};
Node num[MAXN];
bool cmp(Node a, Node b)
{
    if(a.d1 != b.d1) return a.d1 < b.d1;
    else return a.d2 < b.d2;
}
LL dis(LL x1, LL y1, LL x2, LL y2){
    return (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2);
}
int main()
{
    int n; LL x1, y1, x2, y2;
    Ri(n); Rl(x1); Rl(y1); Rl(x2); Rl(y2);
    LL x, y;
    for(int i = 1; i <= n; i++)
        Rl(x), Rl(y), num[i].d1 = dis(x, y, x1, y1), num[i].d2 = dis(x, y, x2, y2);
    LL ans = 1LL * 1e7 * 1e7 * 10; num[0].d1 = 0;
    sort(num, num+n+1, cmp);
    for(int i = 0; i <= n; i++)
    {
        LL Min = 0;
        for(int j = i+1; j <= n; j++)
            Min = max(Min, num[j].d2);
        //Pl(Min + num[i].d1); Pl(ans);
        ans = min(ans, Min + num[i].d1);
    }
    Pl(ans);
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值