CODEFORCES

题目: 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
在这里插入图片描述
思路: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);
    }
}

这里algorithm是c++特有的STL模板的算法头文件 包含了一些特定的算法函数 包括sort(),stable_sort(),partical_sort(),nth_element()等常用的算法函数,C要使用需要加入对应函数的头文件到编译链接的库中;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值