2015
04-14

# Dave

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.

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.

3 2
1 1
2 2
3 3

3

HintIf two people stand in one place, they are embracing. 

 Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author 4602505 2011-09-14 21:37:34 Accepted 4007 109MS 284K 1088 B G++ xym2010
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=1000100000;
struct node
{
int x,y;
}n[1010];
bool cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
int p,r,xmax,xmin,ymax,ymin,y[1010];
while(scanf("%d%d",&p,&r)!=EOF)
{
ymax=xmax=-INF;ymin=xmin=INF;
for(int i=0;i<p;i++)
{
scanf("%d%d",&n[i].x,&n[i].y);
if(ymax<n[i].y)ymax=n[i].y;
if(ymin>n[i].y)ymin=n[i].y;
if(xmax<n[i].x)xmax=n[i].x;
if(xmin>n[i].x)xmin=n[i].x;
}
if((ymax-ymin<=r)&&(xmax-xmin<=r))
{
printf("%d\n",p);
continue;
}
else
{
sort(n,n+p,cmp);
int ans=0;
for(int i=0;i<p;i++)
{
int k=0;
for(int j=i;n[j].x<=n[i].x+r&&j<p;j++)
{
y[k++]=n[j].y;
}
sort(y,y+k);
int count=0,tem=0;
for(int j=0;j<k&&tem<k;j++)
{
while(y[tem]-y[j]<=r&&tem<k)tem++;
if(count<tem-j)count=tem-j;
}
if(ans<count)ans=count;
}
printf("%d\n",ans);
}
}
return 0;
}