描述
Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there are N (1<=N<=1000) people on the ground. Now he wants to know how many people will be in a square with the length of R (1<=R<=1000000000). (Including boundary).
输入
The input contains several cases. For each case there are two positive integers N and R, and then N lines follow. Each gives the (x, y) (1<=x, y<=1000000000) coordinates of people.
输出
Output the largest number of people in a square with the length of R.
样例输入
3 2
1 1
2 2
3 3
样例输出
3
提示
If two people stand in one place, they are embracing.
题意是对于n个点,长为r的正方形区域最多可以覆盖几个点(包括边界)。
总而言之就是暴力,枚举所有x坐标作为左边界,将能在正方形区域内的点存进数组,按y坐标排序,以其作为下边界,统计所有在范围内的点。
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N=5010;
int n, m;
int a[N], b[N];
PII p[N];
signed main()
{
// ios::sync_with_stdio(false);
while(scanf("%lld%lld", &n, &m) != EOF) {
for(int i = 0;i < n;i ++) {
int x, y;
scanf("%lld%lld", &x, &y);
p[i] = {x, y};
a[i] = x;
}
sort(a, a + n);
int ans = 0;
for(int i = 0;i < n;i ++) {
int t = a[i];
int idx = 0;
for(int j = 0;j < n;j ++) {
if(p[j].first <= t + m && p[j].first >= t) {
b[idx ++] = p[j].second;
}
}
sort(b, b + idx);
int cnt = 0;
for(int j = 0;j < idx;j ++) {
while(cnt < idx && b[cnt] - b[j] <= m) cnt ++;
ans = max(ans, cnt - j);
}
}
printf("%d\n", ans);
}
return 0;
}