Dave

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.

这题关键在于先确定x轴上的某一段可行,然后利用1和-1标记对于某一点可行的y(可以想象成先确定了底边,然后根据底边画出一个正方形,然后这个正方形从最低的那个点开始向上移动),对于一个点来说,如果不超过那范围就会一直加在那,如果超过了,那利用-1会把那点减去,这样就可以一直求得可行的点的个数,就可以找出最大值。

#include <stdio.h>
#include<algorithm>
using namespace std;

struct node
{
	int x,y;
}a[1000+10],b[1000+10];

int cmp(node p1,node p2)
{
	if(p1.x!=p2.x)
		return p1.x<p2.x;
	else
		return p1.y<p2.y;
}

int main()
{
	int n,d;
	while(~scanf("%d%d",&n,&d))
	{
		int i,j;
		int max=0;
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&a[i].x,&a[i].y);
		}
		sort(a,a+n,cmp);
		for(i=0;i<n;i++)
		{
			int str=a[i].x,end=a[i].x+d;
			int num=0;
			for(j=0;j<n;j++)
			{
				if(a[j].x>=str&&a[j].x<=end)
				{
					b[num].x=a[j].y;
					b[num].y=1;
					num++;
					b[num].x=a[j].y+d+1;
					b[num].y=-1;
					num++;
				}
			}
			sort(b,b+num,cmp);
			int now=0,maxn=0;
			for(j=0;j<num;j++)
			{
				now+=b[j].y;
				if(now>maxn)
					maxn=now;
			}
			if(maxn>max)
				max=maxn;
		}
		printf("%d\n",max);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值