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.
题目大意:给出n个人的坐标,问长度为r的正方形中最多能够容纳多少人,
解题思路:因为N中有一千,直接暴力枚举,对所有的坐表按x递增的顺序排序,x相同就按y递增的顺序排序,然后从第一个坐标开始,找到第一个坐标x到x+r(正方形的长度)范围内所有点,将其y坐标记录数组y中,然后在枚举,先对y从小到大排序,从第一个点开始,找到y到y+r范围内所有点的个数,这样此时找到的点的个数即为边长为r正方形内的人数,用ans保存最大的一组,最后输出即可
AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int x,y;
}a[1100];
int y[1100];
bool cmp(node a,node b)
{
if(a.x==a.y)
return a.y<b.y;
return a.x<b.x;
}
int main()
{
int i,j,k,n,r;
int maxx,maxy,ans,count,term;
while(cin>>n>>r)
{
ans=0;
for(i=0;i<n;i++)
cin>>a[i].x>>a[i].y;
sort(a,a+n,cmp);
for(i=0;i<n;i++)//x范围内找
{
maxx=a[i].x+r;
count=0;
for(j=i;j<n;j++)
{
if(a[j].x>=a[i].x&&a[j].x<=maxx)
y[count++]=a[j].y;
else
break;
}
sort(y,y+count);
for(j=0;j<count;j++)//在y范围内找
{
maxy=y[j]+r;
term=0;
for(k=j;k<count;k++)
{
if(y[k]>=y[j]&&y[k]<=maxy)
term++;
}
if(term>ans)
ans=term;
}
}
cout<<ans<<endl;
}
return 0;
}