HDU - 4007 勇气

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值