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.
        
 

题目大意:就是给你一个边长为R的正方形,尽可能多的把图中的点圈进去。

题目思路:现找横坐标中符合要求的点,储存起来,在在其中找纵坐标符合要求的点,并且经可能多的选点。

#include<cstdio>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
struct node{
    ll x,y;
    bool friend operator <(node a,node b){
        return a.x<b.x;
    }
}pos[100005];
ll a[100005];
int main(){
    ll n,r;
    while(~scanf("%lld%lld",&n,&r)){
        ll max_x=0,max_y=0,min_x=INF,min_y=INF;
        for(ll i=0;i<n;i++){
            scanf("%lld%lld",&pos[i].x,&pos[i].y);
            max_x=max(max_x,pos[i].x);
            max_y=max(max_y,pos[i].y);
            min_x=min(min_x,pos[i].x);
            min_y=min(min_y,pos[i].y);
        }
        if((max_x-min_x)<=r&&(max_y-min_y)<=r) {
            printf("%lld\n",n);
            continue;
        }
        else{
            //sort(pos,pos+n);这里最好不要用sort否则会超时
            int ans=0;
            //先找符合条件的x再找符合条件的y
            for(int i=0;i<n;i++){
                int k=0;
                for(int j=0;j<n;j++){
                    if(pos[j].x<=r+pos[i].x&&pos[j].x>=pos[i].x) a[k++]=pos[j].y;
                }
                //在符合条件的x里面查找符合条件的y
                sort(a,a+k);
                int cnt=0,temp=0;
                for(int j=0;j<k;j++){
                   while(a[temp]<=a[j]+r&&temp<k) temp++;//从最近的点依次向后查找符合要求的点,因为最近的点所包含的范围的点符合要求,则比当前点远的点也符合要求,所以下一次查找直接从上一次的temp开始
                   ans=max(ans,temp-j);//不断比较去最大值
                }
                //ans=max(ans,cnt);
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值