A flowerbed has many flowers and two fountains.
You can adjust the water pressure and set any values r1(r1 ≥ 0) andr2(r2 ≥ 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 andr2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceedr1, or the distance to the second fountain doesn't exceedr2. 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 andr2 that all the flowers are watered and ther12 + r22 is minimum possible. Find this minimum value.
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 andyi ( - 107 ≤ xi, yi ≤ 107) — the coordinates of the i-th flower.
It is guaranteed that all n + 2 points in the input are distinct.
Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.
2 -1 0 5 3 0 2 5 2
6
4 0 0 5 0 9 4 8 3 -1 0 1 4
33
The first sample is (r12 = 5,r22 = 1): The second sample is (r12 = 1,r22 = 32):
题意:有两个喷泉,半径分别为r1, r2,有n朵花,问在n多花能完全被灌溉的情况下,r1*r1 + r2*r2的最小值是多少?
思路:O(n2):枚举每一朵花到a喷泉的距离作为r1, 然后更新r2的值(不在r1范围内的花都放进r2)。
O(nlogn):按照花与喷泉a之间的距离从小到大排序,(在这个条件下)用一个数组保存花到喷泉b的距离的前缀值(就是第i个位置保存把i~n的花放进喷泉b时,r2的值是多少),然后遍历,更新结果(把前i朵花放进喷泉a,i+1~n朵花放进喷泉b能得到的r1*r1 + r2*r2的最小值)。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define ll long long int
ll dis(ll x1, ll y1, ll x2, ll y2)
{
return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
}
int main()
{
int n;
ll x1, y1, x2, y2, x, y, sum;
ll ra, rb;
scanf("%d", &n);
scanf("%I64d %I64d %I64d %I64d", &x1, &y1, &x2, &y2);
vector< pair<ll, ll> > dist(n);
for(int i = 0;i < n;i++)
{
scanf("%I64d %I64d", &x, &y);
dist[i].first = dis(x, y, x1, y1);
dist[i].second = dis(x, y, x2, y2);
}
sort(dist.begin(), dist.end());
vector<ll> maxRb(n + 1);
for(int i = n - 1;i >= 0;i--)
maxRb[i] = max(maxRb[i + 1], dist[i].second);//前缀
sum = min(maxRb[0], dist[n - 1].first);
for(int i = 0;i < n;i++)
{
ra = dist[i].first;
sum = min(sum, ra + maxRb[i + 1]);
}
printf("%I64d\n", sum);
}
麻烦的考试终于结束。