HDU 4007 Dave (basic algorithm-water question)

Source: Internet
Author: User

Dave
Problem descriptionrecently, 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 ).
Inputthe 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.
 
Outputoutput the largest number of people in a square with the length of R.
Sample Input
3 21 12 23 3
 
Sample output
3HintIf two people stand in one place, they are embracing. 
 
Sourcethe 36th ACM/ICPC Asia Regional Dalian site -- online contest
Recommendlcy | we have carefully selected several similar problems for you: 4003 4008 4005 4009
Question:

I will tell you n points, and then tell you the length of the side of the square. How many points can this square contain at most?


Solution:

First, enumerate the points in the range of X and the efficiency of O (n), and then find the points in the range of X in the range. N is not very large, sort To find the maximum value. Otherwise, the line segment tree is used.


Solution code:

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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.