HDU-4007-(贪心)

HDU-4007-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

大意:输入n和r,然后输入n个坐标,让你输出为r的正方形里最多能有多少个坐标。

思路:对点的x坐标进行排序(y也可以)然后遍历,用数组存放>=x和<=x+r的点的y坐标,对存放的y坐标
进行排序,并不一定是区间y到y+r,它可是上下移动的,找到做多的y坐标就是了,接着遍历,找到在大小为r的区间里的最多的y坐标就是答案。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct stu{
    int x,y;
}de[1010];
bool cmp(stu a,stu b)
{
    return a.x<b.x;
}
int y1[1010];   //存放y坐标 
int main()
{
    int n,r,i,j;
    while(scanf("%d%d",&n,&r)!=EOF)
    {
        memset(y1,0,sizeof(y1));
        for(i=0;i<n;i++)
            scanf("%d%d",&de[i].x,&de[i].y);
        sort(de,de+n,cmp);  //排序 
        int ans,num,l,m,z,max=0;    //ans存放最终结果,其他的都是中转变量 
        for(i=0;i<n;i++){
            num=0,ans=0;
            for(j=i;j<n;j++)    //找到在x和x+r里的y坐标 
            {
                if(de[j].x > de[i].x+r)
                    break;
                y1[num++]=de[j].y;
            }
            sort(y1,y1+num);    //对y坐标排序 
            for(l=0;l<num;l++)
            {
                z=0;        //找到大小为r区间里的最多的坐标个数,用二分函数比较快 
                for(m=l;m<num;m++)
                    if(y1[m]<=y1[l]+r)
                        z++;
                if(z>ans)
                    ans=z;
            }
            if(ans>max)
                max=ans;
        }
        printf("%d\n",max);
    }
    return 0;


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值