617C. 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.

Sample test(s)
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):617C. Watering Flowers【几何】_第1张图片The second sample is (r12 = 1r22 = 32):

617C. Watering Flowers【几何】_第2张图片


遍历每个点作为第二个喷头的半径,然后第一个喷头的半径取剩下的未覆盖的最远的点。这样就能找出最小值。

还有一种预处理的方法是把点按离第一个喷头的远近排好序,然后从后到前预处理一个数组maxd[i]=max(maxd[i+1],p[i].d2);这样maxd[i]存的就是当历遍到第i个点的时候剩下的离第二个喷头最远的点。这样时间复杂度降为nlogn这里就不贴代码了。


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <deque>
#include <list>
using namespace std;
typedef long long ll;

struct node
{
	ll d1;
	ll d2;
}p[2000+10];
int vis[2000+10];
bool cmp(node a, node b)
{
	return a.d1<b.d1;
}
ll sq_len(ll x1,ll y1, ll x2, ll y2)
{
	return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}
int main()
{
	int n;
	int x1,y1,x2,y2;
	scanf("%d%d%d%d%d",&n,&x1,&y1,&x2,&y2);
	for(int i=0 ; i<n ; ++i)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		p[i].d1=sq_len(x,y,x1,y1);
		p[i].d2=sq_len(x,y,x2,y2); 
	}
	p[n].d1=0;
	p[n].d2=0;
	sort(p,p+n+1,cmp);
	ll m;
	bool first=1;
	for(int i=0 ; i<=n ; ++i)
	{
		memset(vis,0,sizeof vis);
		ll r2=p[i].d2,r1=0;
		for(int j=0 ; j<=n ; ++j)
		{
			if(p[j].d2<=r2 )
				vis[j]=1;
		}
		for(int j=n ; j>=0 ; --j)
		{
			if(!vis[j])
			{
				r1=p[j].d1;
				break;
			}
		} 
		if(first)
		{
			m=r1+r2;
			first=0;
		}
		else
		{
			m=min(m,r1+r2);
		}
	}
	printf("%I64d\n",m);
	return 0;
}


你可能感兴趣的:(编程,C++,算法,ACM)