617C. Watering Flowers【几何】
2016-01-25 15:08
351 查看
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 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.
Sample test(s)
input
output
input
output
Note
The first sample is (r12 = 5, r22 = 1):
The
second sample is (r12 = 1, r22 = 32):
遍历每个点作为第二个喷头的半径,然后第一个喷头的半径取剩下的未覆盖的最远的点。这样就能找出最小值。
还有一种预处理的方法是把点按离第一个喷头的远近排好序,然后从后到前预处理一个数组maxd[i]=max(maxd[i+1],p[i].d2);这样maxd[i]存的就是当历遍到第i个点的时候剩下的离第二个喷头最远的点。这样时间复杂度降为nlogn这里就不贴代码了。
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 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.
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 = 5, r22 = 1):
The
second sample is (r12 = 1, r22 = 32):
遍历每个点作为第二个喷头的半径,然后第一个喷头的半径取剩下的未覆盖的最远的点。这样就能找出最小值。
还有一种预处理的方法是把点按离第一个喷头的远近排好序,然后从后到前预处理一个数组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 .d1=0; p .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++实现JNI接口需要注意的事项
- 关于指针的一些事情
- 肯特·贝克:改变人生的代码整理魔法
- 你应该学习哪种编程语言?
- c++ primer 第五版 笔记前言
- [转]我们需要一种其他人能使用的编程语言
- share_ptr的几个注意点
- 书评:《算法之美( Algorithms to Live By )》
- DB2编程序技巧(1)
- DB2编程序技巧 (四)
- 女人VS编程_国庆快乐
- DB2编程序技巧 (六)
- DB2编程序技巧 (三)
- DB2编程序技巧 (九)
- DB2编程序技巧 (七)
- DB2编程序小小技巧
- DB2编程序技巧 (五)
- 动易2006序列号破解算法公布
- DB2编程序技巧 (一)
- DB2编程序技巧 (八)