CodeForces - 617C ( 思维 + 暴力求解)

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.

题意:就是两个喷泉,喷泉附近有很多花,每个喷泉有一个圆形的覆盖范围,分别为r1, r2,要使所有花再两个圆之内,求r1 * r1 + r2 * r2 的最小值

思路:本来以为是什么计算几何题。。。一看n才2000.。。直接暴力枚举就好了。先把所有话距离第一个喷泉的距离从小到大排序,然后从大到小决定r1的值,然后再求出未被r1覆盖的所有花中距离第二个喷泉最远的那个为r2。求出最小的r1 + r2 就好了

#include <vector>
#include <stdio.h>
#include <map>
#include <stdlib.h>
#include<ctype.h>
#include <iostream>
#include <queue>
#include <algorithm>
#define LL long long
using namespace std;

const int MAX = 1e5 + 50;
LL INF = 0x3f3f3f3f3f3f3f3f;
LL n, x1, y1, x2, y2;
LL cal1(LL x, LL y){ // 算距离喷泉的距离
    return (x - x1) * (x - x1) + (y - y1) * (y - y1);
}

LL cal2(LL x, LL y){
    return (x - x2) * (x - x2) + (y - y2) * (y - y2);
}

struct date // 记录到每个喷泉的距离
{
    LL d1;
    LL d2;
}arr[2005];

bool cmp(date A, date B){
    return A.d1 < B.d1;
}
int main(int argc, char const *argv[]){ 
    
    int k = 0;
    scanf("%lld%lld%lld%lld%lld", &n, &x1, &y1, &x2, &y2);
    LL r1 = INF;
    LL r2 = INF;
    for(int i = 0; i < n; i++){
        LL x, y;
        scanf("%lld%lld", &x, &y);
        LL d1 = cal1(x, y);
        LL d2 = cal2(x, y);
        arr[k].d1 = d1;
        arr[k++].d2 = d2;
    }
    sort(arr, arr + k, cmp);
    for(int i = k - 1; i >= 0; i--){
        LL d1 = arr[i].d1;
        LL d2 = 0;
        for(int j = k - 1; j > i; j--){ 求出剩下的花中距离第二个喷泉最远的
            d2 = max(d2, arr[j].d2);
        }

        if(r1 + r2 > d1 + d2){
            r1 = d1;
            r2 = d2;
        }
    }
    LL ma = 0;
    for(int i = 0; i < k; i++){ // 再额外判断一第一个喷泉不喷水的情况
        ma = max(ma, arr[i].d2);
    }
    if(n == 1){
        printf("%lld\n", min(arr[0].d1, arr[0].d2));
    } else{
        printf("%lld\n", min(r1 + r2, ma));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43737952/article/details/89007945