Codeforcces 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 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.

Examples
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*r1+r2*r2最小。


思路:


1、首先我们做出每个点到圆心1的距离记为dis1,和道圆心2的距离记为dis2。


2、然后我们按照dis1降序排序,排序后,如果我们选择第i个点去用圆1来覆盖,那么其实从i+1-n个点都可以用圆1来覆盖了(因为dis1是降序的),那么从第1个点到第i-1个点就需要用圆2来覆盖,那么我们这个时候我们只需要维护一下从第1个点到第i-1个点的dis2最大值即可,那么当前枚举出来的情况的解就是a【i】.dis1+maxn(这个维护的最大值)


3、按照dis1降序排序之后,我们再按照dis2降序排序,按照上述方式同理维护一个最优解即可。注意数据范围比较大,数据类型和初始化要注意一下。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define ll __int64
struct node
{
    ll dis1,dis2;
}a[12121];
ll dis(ll x,ll y,ll x2,ll y2)
{
    ll tmp1=(x-x2)*(x-x2)+(y-y2)*(y-y2);
    return tmp1;
}
int cmp(node a,node b)
{
    if(a.dis2==b.dis2)return a.dis1>b.dis1;
    else return a.dis2>b.dis2;
}
int cmp2(node a,node b)
{
    if(a.dis1==b.dis1)return a.dis2>b.dis2;
    else return a.dis1>b.dis1;
}
int main()
{
    int n;
    ll x1,y1,x2,y2;
    while(~scanf("%d%I64d%I64d%I64d%I64d",&n,&x1,&y1,&x2,&y2))
    {
        for(int i=0;i<n;i++)
        {
            ll x,y;
            scanf("%I64d%I64d",&x,&y);
            a[i].dis1=dis(x,y,x1,y1);
            a[i].dis2=dis(x,y,x2,y2);
        }
        sort(a,a+n,cmp);
        ll ans=100000000000000000;
        for(int i=0;i<n;i++)
        {
            ll tmpmaxn=0;
            for(int j=0;j<i;j++)
            {
                tmpmaxn=max(tmpmaxn,a[j].dis1);
            }
            ans=min(ans,tmpmaxn+a[i].dis2);
        }
        sort(a,a+n,cmp2);
        for(int i=0;i<n;i++)
        {
            ll tmpmaxn=0;
            for(int j=0;j<i;j++)
            {
                tmpmaxn=max(tmpmaxn,a[j].dis2);
            }
            ans=min(ans,tmpmaxn+a[i].dis1);
        }
        printf("%I64d\n",ans);
    }
}




### 解题思路 在Codeforces 617C浇花问题里,目标是确定在给定天数、花的初始高度、每次可浇水的连续花的数量等条件下,能让最小高度的花达到的最大高度。这是一个优化问题,核心在于找到一种浇水策略,使得最终最小高度的花尽可能高。 一般可采用二分查找的方法来解决。先确定最小高度花的可能取值范围,也就是从当前最小高度到一个理论上可能达到的最大高度。然后在这个范围内进行二分查找,对于每一个中间值,检查是否能够通过合理的浇水操作让所有花的高度都不低于这个中间值。 检查的过程是模拟浇水操作,从左到右遍历每一朵花,如果某朵花的高度低于当前检查的中间值,就从这朵花开始往后选择连续的 `w` 朵花进行浇水,直到这朵花达到中间值。同时要记录浇水的次数,若浇水次数超过了给定的天数 `m`,则说明这个中间值无法达到,需要缩小右边界;反之,则可以尝试更大的值,扩大左边界。 ### 代码实现 ```python # 检查是否能让所有花的高度都不低于 h def can_reach_height(heights, m, w, h): n = len(heights) watered = [0] * n total_watering = 0 current_watering = 0 for i in range(n): # 计算当前位置的实际浇水情况 if i - w >= 0: current_watering -= watered[i - w] need = max(0, h - (heights[i] + current_watering)) watered[i] = need current_watering += need total_watering += need if total_watering > m: return False return True # 二分查找最大的最小高度 def find_max_min_height(heights, m, w): left = min(heights) right = min(heights) + m while left < right: mid = (left + right + 1) // 2 if can_reach_height(heights, m, w, mid): left = mid else: right = mid - 1 return left # 读取输入 n, m, w = map(int, input().split()) heights = list(map(int, input().split())) # 计算并输出结果 result = find_max_min_height(heights, m, w) print(result) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值