Dave
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 4210 Accepted Submission(s): 1432
Problem Description
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).
Input
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
Output the largest number of people in a square with the length of R.
Sample Input
3 2 1 1 2 2 3 3
Sample Output
3
Hint
If two people stand in one place, they are embracing.
Source
Recommend
lcy
题意:
给了坐标轴上的点,问边长为R的正方形最多能包含多少个点。
思路:
因为N只有1000,所以可以考虑暴力。
先按Y轴,把符合在正方形的上下边界以内的值全部记录下来,然后再按X轴找到最多能覆盖的点。(这里可以用排序来优化一下)
时间复杂度为O(N^2*logN)。
注意,无穷大要大于2e9,因为x+r可以达到2e9。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1005;
const int inf=2000000005;
struct node
{
int x,y;
}q[maxn];
int x[maxn];
int main()
{
int n,r;
while(~scanf("%d%d",&n,&r))
{
int ans=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&q[i].x,&q[i].y);
}
for(int i=1;i<=n;i++)
{
int cntx=0;
for(int j=1;j<=n;j++)
{
if(q[j].y<=q[i].y+r&&q[j].y>=q[i].y)
{
x[cntx++]=q[j].x;
}
}
sort(x,x+cntx);
x[cntx++]=inf;
int pos=0;
for(int j=0;j<cntx-1;j++)
{
while(x[pos]<=x[j]+r) pos++;
ans=max(ans,(pos-j));
}
}
printf("%d\n",ans);
}
return 0;
}