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

The 36th ACM/ICPC
Asia Regional Dalian Site —— Online Contest

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

时间: 2024-12-25 14:30:06

HDU 4007 Dave (基本算法-水题)的相关文章

HDU 4006 The kth great number (基本算法-水题)

The kth great number Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too mu

HDU 4022 Bombing(基本算法-水题)

Bombing Problem Description It's a cruel war which killed millions of people and ruined series of cities. In order to stop it, let's bomb the opponent's base. It seems not to be a hard work in circumstances of street battles, however, you'll be encou

hdu 1999 不可摸数 水题。

不可摸数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7966    Accepted Submission(s): 2024 Problem Description s(n)是正整数n的真因子之和,即小于n且整除n的因子和.例如s(12)=1+2+3+4+6=16.如果任何数m,s(m)都不等于n,则称n为不可摸数. Input 包

HDU Senior&#39;s Gun (水题)

题意:给n把枪,m个怪兽,每把枪可消灭1怪兽,并获得能量=枪的攻击力-怪兽的防御力.求如何射杀能获得最多能量?(不必杀光) 思路:用最大攻击力的枪杀防御力最小的怪兽明显可获得最大能量.如果每把枪都去射杀刚好1点能量都拿不到的怪物,那简直等于把枪全丢掉. 1 //#pragma comment(linker,"/STACK:102400000,102400000") 2 #include <iostream> 3 #include <stdio.h> 4 #inc

HDU 5590 ZYB&#39;s Biology 水题

ZYB's Biology Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5590 Description ZYB(ZJ−267)在NOIP拿到600分之后开始虐生物题,他现在扔给你一道简单的生物题:给出一个DNA序列和一个RNA序列,问它们是否配对. DNA序列是仅由A,C,G,T组成的字符串,RNA序列是仅由A,C,G,U组成的字符串. DNA和RNA匹配当且仅当每

hdu 4847 Wow! Such Doge! 水题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4847 统计文本中一共有多少个“Doge” 水题 #include <cstring> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <cstdio> #includ

HDU 5578 Friendship of Frog 水题

Friendship of Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5578 Description N frogs from different countries are standing in a line. Each country is represented by a lowercase letter. The distance betwee

杭电(hdu)2053 Switch Game 水题

Switch Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13113    Accepted Submission(s): 7970 Problem Description There are many lamps in a line. All of them are off at first. A series of o

HDU 1846 Brave Game (博弈水题)

题意:中文...你们懂得. 析:这个就是一个水题博弈,就是一个巴什博弈定理,直接就没有变,如果你们看过我写的那个,这个题绝对水过. 附地址:http://www.cnblogs.com/dwtfukgv/p/5517818.html 看完后就懂了吧,不用说了,直接上代码就OK. 代码如下: #include <iostream> #include <string> #include <vector> #include <algorithm> #include