本文主要是介绍【Codeforces Round 340 (Div 2)C】【暴力排序枚举】Watering Flowers 2个灌溉器灌溉所有点最小的rr+RR,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 thei-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):
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 2020, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n;
LL X1, Y1, X2, Y2;
LL x[N], y[N];
LL K(LL x) { return x*x; }
LL check(LL r)
{r = r*r;LL R=0;for (int i = 1; i <= n; ++i){if (K(x[i] - X1) + K(y[i] - Y1) <= r);else{gmax(R, K(x[i] - X2) + K(y[i] - Y2));}}return r+R;
}
struct A
{LL x, y, d, D;bool operator < (const A& b)const {return d < b.d;}
}a[N];
int main()
{while (~scanf("%d", &n)){scanf("%lld%lld%lld%lld", &X1,&Y1,&X2,&Y2);for (int i = 1; i <= n; ++i){scanf("%lld%lld", &a[i].x, &a[i].y);a[i].d = K(a[i].x - X1) + K(a[i].y - Y1);a[i].D = K(a[i].x - X2) + K(a[i].y - Y2);}sort(a + 1, a + n + 1); a[0].d = 0;LL ans = 1e18;for (int i = 0; i <= n; ++i){LL R = 0;for (int j = i + 1; j <= n; ++j)gmax(R, a[j].D);gmin(ans, a[i].d + R);}printf("%lld\n", ans);}return 0;
}
/*
【trick&&吐槽】
最近做题太不认真了,这题我竟然没怎么想清楚就写了三分。
写完才发现样例都跑不出。实在是太蠢了!
三分是错误的。
因为在r位于[a[p].d a[p+1].d]范围内时,可能当r为两侧(a[p].d a[p+1].d)的权值时更优。
不满足整体单峰性。【题意】
有2个灌溉器,坐标分别在(X1,Y1)和(X2,Y2)
有n个点需要被灌溉,给出你坐标。
问你,我们如何设置这2个灌溉器的灌溉半径r与R,才能使得所有的点都被灌溉到。
且r^2+R^2尽可能小。【类型】
暴力排序枚举 or 三分【分析】
很显然,r和R都是恰好灌溉到距离其最远的点就好了。
然而,这不意味着r和R是整数。
只意味着r^2与R^2是整数。我们发现点数n很小,只有2000
于是自然想到,我们可以暴力。
如果我们把每个点到1号灌溉源距离的平方设为d,到2号灌溉源距离的平方设为D
那么,我们枚举r^2,只要把所有的d排成升序依次枚举即可。
剩下的灌溉不到的,让2号灌溉源处理。【时间复杂度&&优化】
O(n^2)*/
这篇关于【Codeforces Round 340 (Div 2)C】【暴力排序枚举】Watering Flowers 2个灌溉器灌溉所有点最小的rr+RR的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!