Codeforces Round #340 (Div. 2)C. Watering Flowers
来源:互联网 发布:java聊天室私聊的代码 编辑:程序博客网 时间:2024/05/22 13:27
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.
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.
Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.
2 -1 0 5 30 25 2
6
4 0 0 5 09 48 3-1 01 4
33
The first sample is (r12 = 5, r22 = 1):The second sample is (r12 = 1, r22 = 32):
题意:给出两个圆心,半径分别为r1,r2,然后再给出一些点,问r1^2+r2^2最小为多少能是所有的点至少在一个圆中
方法一:
预处理出所有点到圆心一的距离,到圆心二的距离,从小到大排序
枚举第一个圆心的半径,二分第二个圆心的半径,二分的时候判断是否所有的点都在圆内
时间复杂度:n^2*logn
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <ctime>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define PI acos(-1.0)#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define mem(a, b) memset(a, b, sizeof(a))typedef long long ll;struct node{ __int64 x,y;}a[2010];__int64 ans1[2010],ans2[2010];__int64 x1,x2,y,y2;bool solve(int n,__int64 num1,__int64 num2){ for(int i=1;i<=n;i++){ if((a[i].x-x1)*(a[i].x-x1)+(a[i].y-y)*(a[i].y-y)>num1&&(a[i].x-x2)*(a[i].x-x2)+(a[i].y-y2)*(a[i].y-y2)>num2) return false; } return true;}int main(){ int n; while(scanf("%d%I64d%I64d%I64d%I64d",&n,&x1,&y,&x2,&y2)!=EOF){ for(int i=1;i<=n;i++){ scanf("%I64d%I64d",&a[i].x,&a[i].y); ans1[i]=(a[i].x-x1)*(a[i].x-x1)+(a[i].y-y)*(a[i].y-y); ans2[i]=(a[i].x-x2)*(a[i].x-x2)+(a[i].y-y2)*(a[i].y-y2); } __int64 ans=10000000000000000ll; ans1[0]=0; ans2[0]=0; sort(ans1+1,ans1+n+1); sort(ans2+1,ans2+n+1); int low=n,high=n; for(int i=0;i<=n;i++){ high=low,low=0; while(high-low>=0){ int mid=(low+high)>>1; if(solve(n,ans1[i],ans2[mid])) high=mid-1; else low=mid+1; } ans=min(ans,ans1[i]+ans2[low]); } printf("%I64d\n",ans); } return 0;}
方法二:
把所有的点按到圆心一的距离从小到大排序,枚举剩下的点到第二个点的最大距离
时间复杂度n^2
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <ctime>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define PI acos(-1.0)#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define mem(a, b) memset(a, b, sizeof(a))typedef long long ll;struct node{ __int64 x,y;}a[2010];__int64 x1,x2,y,y2;bool cmp(node u,node v){ return (u.x-x1)*(u.x-x1)+(u.y-y)*(u.y-y)<(v.x-x1)*(v.x-x1)+(v.y-y)*(v.y-y);}int main(){ int n; while(scanf("%d%I64d%I64d%I64d%I64d",&n,&x1,&y,&x2,&y2)!=EOF){ for(int i=1;i<=n;i++) scanf("%I64d%I64d",&a[i].x,&a[i].y); __int64 ans=10000000000000000ll; sort(a+1,a+n+1,cmp); for(int i=0;i<=n;i++){ __int64 ans1=(a[i].x-x1)*(a[i].x-x1)+(a[i].y-y)*(a[i].y-y),ans2=0; if(i==0) ans1=0; for(int j=i+1;j<=n;j++) ans2=max(ans2,(a[j].x-x2)*(a[j].x-x2)+(a[j].y-y2)*(a[j].y-y2)); ans=min(ans,ans1+ans2); } printf("%I64d\n",ans); } return 0;}
方法三:
把所有的点按到圆心一的距离从小到大排序,先预处理出i+1到n距离第二个圆心的最短距离
时间复杂度:n*logn
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <ctime>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define PI acos(-1.0)#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define mem(a, b) memset(a, b, sizeof(a))typedef long long ll;struct node{ __int64 x,y;}a[2010];__int64 x1,x2,y,y2;__int64 ans2[2010];bool cmp(node u,node v){ return (u.x-x1)*(u.x-x1)+(u.y-y)*(u.y-y)<(v.x-x1)*(v.x-x1)+(v.y-y)*(v.y-y);}int main(){ int n; while(scanf("%d%I64d%I64d%I64d%I64d",&n,&x1,&y,&x2,&y2)!=EOF){ for(int i=1;i<=n;i++) scanf("%I64d%I64d",&a[i].x,&a[i].y); __int64 ans=10000000000000000ll; sort(a+1,a+n+1,cmp); ans2[n+1]=0; for(int i=n;i>=1;i--) ans2[i]=max(ans2[i+1],(a[i].x-x2)*(a[i].x-x2)+(a[i].y-y2)*(a[i].y-y2)); for(int i=0;i<=n;i++){ __int64 ans1=(a[i].x-x1)*(a[i].x-x1)+(a[i].y-y)*(a[i].y-y); if(i==0) ans1=0; ans=min(ans,ans1+ans2[i+1]); } printf("%I64d\n",ans); } return 0;}
- Codeforces Round #340 (Div. 2) C. Watering Flowers
- Codeforces Round #340 (Div. 2) C. Watering Flowers
- Codeforces Round #340 (Div. 2)C. Watering Flowers
- Codeforces Round #340 (Div. 2)C. Watering Flowers(暴力)
- Codeforces Round #340 (Div. 2)C. Watering Flowers
- Codeforces Round #340 (Div. 2)-C. Watering Flowers
- Codeforces 617C Watering Flowers (Round #340 (Div. 2) C题) nlogn算法
- Codeforces Round #340 (Div. 2) 617C Watering Flowers(计算几何)
- Codeforces Round #340 (Div. 2) C. Watering Flowers(暴力枚举)
- Codeforces Round #340 (Div. 2) C. Watering Flowers 计算几何、圆和点
- Codeforces Round #340 -C - Watering Flowers-暴力+set
- 【Codeforces Round 340 (Div 2)C】【暴力排序枚举】Watering Flowers 2个灌溉器灌溉所有点最小的rr+RR
- Codeforces 617 C. Watering Flowers
- Codeforces 617C:Watering Flowers
- Codeforces 617C(Watering Flowers)
- CodeForces 617C Watering Flowers
- Codeforces Round #341 (Div. 2) C. Wet Shark and Flowers
- Codeforces Round #341 (Div. 2)--C. Wet Shark and Flowers
- 块级元素和内联元素的区别小结
- POJ 2409 Let it Bead(polya)
- 清除行列
- python中print的嚣技巧
- Java 方法
- Codeforces Round #340 (Div. 2)C. Watering Flowers
- java回调再回首
- C#中的IDisposable模式用法详解
- Think in Java反刍笔记(4)---一切都是对象(第二章)
- java 常用算法和时间复杂度
- 用户名 不在 sudoers文件中,此事将被报告。
- 1082. Read Number in Chinese (25)
- Android性能分析工具整理汇总
- android sdk目录中工具介绍