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).

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
#include <stdio.h> 
#include<algorithm>
#include <string.h>
#include <algorithm>
using namespace std; 
#define N 1006
int n,r;
struct node{
	int x, y;
}p[N];
int yy[N];int xx[N];
int main()
{
	while(scanf("%d%d",&n,&r)!=EOF)
	{
		memset(yy,0,sizeof(yy));
		memset(xx,0,sizeof(xx));
			for(int i=0;i<n;i++)
		{
			scanf("%d%d",&p[i].x,&p[i].y);
			yy[i]=p[i].y;
		}
		sort(yy,yy+n);
		int ans=0;
		for(int i=0;i<n;i++)
		{
			int xcnt=0;
			for(int j=0;j<n;j++)
			{
				if(p[j].y<=yy[i]+r&&p[j].y>=yy[i]){
					xx[xcnt++]=p[j].x;
				}
			}
			sort(xx,xx+xcnt);
			int e=0;
			for(int j=0;j<xcnt;j++){
				while(xx[e]<=xx[j]+r&&e<xcnt) e++;//j从0开始就相当于| | 		的数量由于都是数组xx[],用e-j表示个数 
				ans=max(ans,e-j);				//				xx[j] xx[j]+r				|        |
			}									//										xx[j] XX[e]	 xx[j]+r 所以说(e-j)表示个数 
			//printf("%d\n",ans);
			
		}
			printf("%d\n",ans);
	}
		return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42434171/article/details/81367832
今日推荐