码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-03 23:17:36      阅读:398      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   for   ar   

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




HDU 4007 Dave (基本算法-水题),布布扣,bubuko.com

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

标签:des   style   http   color   os   io   for   ar   

原文地址:http://blog.csdn.net/a1061747415/article/details/38362337

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!