2020 BIT冬训-C++STL I - Watering Flowers CodeForces - 617C
Problem Description
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
2 -1 0 5 3
0 2
5 2
6
4 0 0 5 0
9 4
8 3
-1 0
1 4
33
这题的思路是暴力枚举。先对到一个水池的距离进行排序。因此不在这个水池范围内的花一定在另一个水池内。
由于已经排好序(降序),所以只需要寻找该数据前的数据中与另一个水池中心的最大距离。
需要注意的是不要忽略了所有花都在一个水池而另一个水池一朵花都没有的情况。
这题一开始我是用贪心做的。但是如果出现一朵花在两个水池中点,就会出错。
#include<stdio.h> #include<algorithm> using namespace std; typedef pair<long long,long long> pii; long long n,dis1,dis2=0x3f3f3f3f,x,y; pii foun[2]; struct flower{ long long id,fdis1,fdis2; }flow[2005]; int cmp(const struct flower & a,const struct flower & b){ return a.fdis2>b.fdis2; } int main(){ scanf("%lld",&n); scanf("%lld%lld%lld%lld",&foun[0].first,&foun[0].second,&foun[1].first,&foun[1].second); for(int i=0;i<n;i++){ scanf("%lld%lld",&x,&y); flow[i].fdis1 = (x-foun[0].first)*(x-foun[0].first)+(y-foun[0].second)*(y-foun[0].second); flow[i].fdis2 = (x-foun[1].first)*(x-foun[1].first)+(y-foun[1].second)*(y-foun[1].second); } sort(flow,flow+n,cmp); dis2=flow[0].fdis2; for(int i=1;i<n;i++){ dis1=0; for(int j=0;j<i;j++){ if(dis1<flow[j].fdis1){ dis1=flow[j].fdis1; } } dis2=min(dis2,dis1+flow[i].fdis2); } if(dis1<flow[n-1].fdis1) dis1=flow[n-1].fdis1; dis2=min(dis2,dis1); printf("%lld",dis2); }

浙公网安备 33010602011771号