Dave
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 387 Accepted Submission(s): 107
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
3HintIf two people stand in one place, they are embracing.
Source
Recommend
lcy
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define N 1010
struct Point
{
int x,y;
}P[N];
int Y[N],J;
int n,r;
bool cmp(Point a,Point b)
{
return a.x<a.x;
}
void getY(int x1,int x2)//得到P[i].x-P[i].x+r的点的y坐标
{
int i;
J=0;
for(i=0;i<n;i++)
{
if(P[i].x>=x1&&P[i].x<=x2)
Y[J++]=P[i].y;
}
sort(Y,Y+J);
}
int how()//查找当前Y[]中的矩形可圈住的点的个数最大值
{
int ans=0;
int ans1=0;
int i,j,k;
int low=0;
for(i=0;i<J;i++)
{
while(Y[i]-Y[low]>r)low++;//设置两个指针标记i,low
ans=i+1-low;
if(ans1<ans)ans1=ans;
}
return ans1;
}
int main()
{
int i,j,k,ans,ans1;
while(scanf("%d%d",&n,&r)!=EOF)
{
ans1=ans=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&P[i].x,&P[i].y);
}
sort(P,P+n,cmp);
int tt = -1;
for(i=0;i<n;i++)
{
if(P[i].x == tt) continue;
tt = P[i].x;
getY(P[i].x,P[i].x+r);
ans1=how();
if(ans<ans1)ans=ans1;
}
printf("%d\n",ans);
}
return 0;
}
/*
6 2
1 1
1 3
2 1
2 3
3 1
3 3
*/