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

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 = 5r22 = 1):The second sample is (r12 = 1r22 = 32):


题意:给出两个圆心,半径分别为r1,r2,然后再给出一些点,问r1^2+r2^2最小为多少能是所有的点至少在一个圆中

方法一:

预处理出所有点到圆心一的距离,到圆心二的距离,从小到大排序

枚举第一个圆心的半径,二分第二个圆心的半径,二分的时候判断是否所有的点都在圆内

时间复杂度:n^2*logn

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI acos(-1.0)
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
struct node{
    __int64 x,y;
}a[2010];
__int64 ans1[2010],ans2[2010];
__int64 x1,x2,y,y2;

bool solve(int n,__int64 num1,__int64 num2){
    for(int i=1;i<=n;i++){
        if((a[i].x-x1)*(a[i].x-x1)+(a[i].y-y)*(a[i].y-y)>num1&&(a[i].x-x2)*(a[i].x-x2)+(a[i].y-y2)*(a[i].y-y2)>num2)
            return false;
    }
    return true;
}

int main(){
    int n;
    while(scanf("%d%I64d%I64d%I64d%I64d",&n,&x1,&y,&x2,&y2)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%I64d%I64d",&a[i].x,&a[i].y);
            ans1[i]=(a[i].x-x1)*(a[i].x-x1)+(a[i].y-y)*(a[i].y-y);
            ans2[i]=(a[i].x-x2)*(a[i].x-x2)+(a[i].y-y2)*(a[i].y-y2);
        }
        __int64 ans=10000000000000000ll;
        ans1[0]=0;
        ans2[0]=0;
        sort(ans1+1,ans1+n+1);
        sort(ans2+1,ans2+n+1);
        int low=n,high=n;
        for(int i=0;i<=n;i++){
            high=low,low=0;
            while(high-low>=0){
                int mid=(low+high)>>1;
                if(solve(n,ans1[i],ans2[mid]))
                    high=mid-1;
                else
                    low=mid+1;
            }
            ans=min(ans,ans1[i]+ans2[low]);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

方法二:

把所有的点按到圆心一的距离从小到大排序,枚举剩下的点到第二个点的最大距离

时间复杂度n^2

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI acos(-1.0)
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
struct node{
    __int64 x,y;
}a[2010];
__int64 x1,x2,y,y2;

bool cmp(node u,node v){
    return (u.x-x1)*(u.x-x1)+(u.y-y)*(u.y-y)<(v.x-x1)*(v.x-x1)+(v.y-y)*(v.y-y);
}

int main(){
    int n;
    while(scanf("%d%I64d%I64d%I64d%I64d",&n,&x1,&y,&x2,&y2)!=EOF){
        for(int i=1;i<=n;i++)
            scanf("%I64d%I64d",&a[i].x,&a[i].y);
        __int64 ans=10000000000000000ll;
        sort(a+1,a+n+1,cmp);
        for(int i=0;i<=n;i++){
            __int64 ans1=(a[i].x-x1)*(a[i].x-x1)+(a[i].y-y)*(a[i].y-y),ans2=0;
            if(i==0)
                ans1=0;
            for(int j=i+1;j<=n;j++)
                ans2=max(ans2,(a[j].x-x2)*(a[j].x-x2)+(a[j].y-y2)*(a[j].y-y2));
            ans=min(ans,ans1+ans2);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

方法三:

把所有的点按到圆心一的距离从小到大排序,先预处理出i+1到n距离第二个圆心的最短距离

时间复杂度:n*logn

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI acos(-1.0)
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
struct node{
    __int64 x,y;
}a[2010];
__int64 x1,x2,y,y2;
__int64 ans2[2010];

bool cmp(node u,node v){
    return (u.x-x1)*(u.x-x1)+(u.y-y)*(u.y-y)<(v.x-x1)*(v.x-x1)+(v.y-y)*(v.y-y);
}

int main(){
    int n;
    while(scanf("%d%I64d%I64d%I64d%I64d",&n,&x1,&y,&x2,&y2)!=EOF){
        for(int i=1;i<=n;i++)
            scanf("%I64d%I64d",&a[i].x,&a[i].y);
        __int64 ans=10000000000000000ll;
        sort(a+1,a+n+1,cmp);
        ans2[n+1]=0;
        for(int i=n;i>=1;i--)
            ans2[i]=max(ans2[i+1],(a[i].x-x2)*(a[i].x-x2)+(a[i].y-y2)*(a[i].y-y2));
        for(int i=0;i<=n;i++){
            __int64 ans1=(a[i].x-x1)*(a[i].x-x1)+(a[i].y-y)*(a[i].y-y);
            if(i==0)
                ans1=0;
            ans=min(ans,ans1+ans2[i+1]);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值