Codeforces Round #340 (Div. 2) C. 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) andr2(r2 ≥ 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 andr2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceedr1, or the distance to the second fountain doesn't exceedr2. 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 andr2 that all the flowers are watered and ther12 + 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 andyi ( - 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 = 5,r22 = 1): The second sample is (r12 = 1,r22 = 32):

题意:有两个喷泉,半径分别为r1, r2,有n朵花,问在n多花能完全被灌溉的情况下,r1*r1 + r2*r2的最小值是多少?

思路:O(n2):枚举每一朵花到a喷泉的距离作为r1, 然后更新r2的值(不在r1范围内的花都放进r2)。

           O(nlogn):按照花与喷泉a之间的距离从小到大排序,(在这个条件下)用一个数组保存花到喷泉b的距离的前缀值(就是第i个位置保存把i~n的花放进喷泉b时,r2的值是多少),然后遍历,更新结果(把前i朵花放进喷泉a,i+1~n朵花放进喷泉b能得到的r1*r1 + r2*r2的最小值)。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define ll long long int
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, x, y, sum;
    ll ra, rb;
    scanf("%d", &n);
    scanf("%I64d %I64d %I64d %I64d", &x1, &y1, &x2, &y2);
    vector< pair<ll, ll> > dist(n);
    for(int i = 0;i < n;i++)
    {
        scanf("%I64d %I64d", &x, &y);
        dist[i].first = dis(x, y, x1, y1);
        dist[i].second = dis(x, y, x2, y2);
    }
    sort(dist.begin(), dist.end());
    vector<ll> maxRb(n + 1);
    for(int i = n - 1;i >= 0;i--)
        maxRb[i] = max(maxRb[i + 1], dist[i].second);//前缀
    sum = min(maxRb[0], dist[n - 1].first);
    for(int i = 0;i < n;i++)
    {
        ra = dist[i].first;
        sum = min(sum, ra + maxRb[i + 1]);
    }
    printf("%I64d\n", sum);
}

麻烦的考试终于结束。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值