HDU 4007 Dave (基本算法-水题)

Dave


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

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   4003  4008  4005  4009  4010 
 

题目大意:

告诉你n个点,再告诉你正方形边长,问你这个正方形最多包含多少个点?


解题思路:

先枚举x在范围内的点,O(n)的效率,然后对x在范围的点中,找出y在范围内的点,这个考虑到n不是很大,排序找出最大的即可,否则要用到线段树。


解题代码:

#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=1100;

int n,l;

struct point{
    int x,y;
    point(int x0=0,int y0=0){
        x=x0;y=y0;
    }
    friend bool operator < (point p1,point p2){
        if(p1.x!=p2.x) return p1.x<p2.x;
        else return p1.y<p2.y;
    }
}p[maxn];

bool cmp(point p1,point p2){
    return p1.y<p2.y;
}

int getCnt(vector <point> v){
    sort(v.begin(),v.end(),cmp);
    queue <int> q;
    int tmp=0;
    for(int i=0;i<v.size();i++){
        if(!q.empty()){
            int s=q.front();
            if(v[i].y-v[s].y<=l){
                q.push(i);
            }else{
                q.pop();
                i--;
            }
        }else{
            q.push(i);
        }
        if(q.size()>tmp) tmp=q.size();
    }
    return tmp;
}

void solve(){
    int ans=0;
    sort(p,p+n);
    queue <int> q;
    for(int i=0;i<n;i++){
        if(!q.empty()){
            int s=q.front();
            if(p[i].x-p[s].x<=l){
                q.push(i);
            }else{
                vector <point> v;
                int qsize=q.size();
                while(qsize-- >0){
                    int s=q.front();
                    q.pop();
                    v.push_back(p[s]);
                    q.push(s);
                }
                int tmp=getCnt(v);
                if(tmp>ans) ans=tmp;
                q.pop();
                i--;
            }
        }else{
            q.push(i);
        }
    }
    vector <point> v;
    while(!q.empty()){
        int s=q.front();
        q.pop();
        v.push_back(p[s]);
    }
    int tmp=getCnt(v);
    if(tmp>ans) ans=tmp;
    printf("%d\n",ans);
}

int main(){
    while(scanf("%d%d",&n,&l)!=EOF){
        for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y);
        solve();
    }
    return 0;
}




转载于:https://www.cnblogs.com/toyking/p/3893150.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值