Dave 题解

描述

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

输入

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 the largest number of people in a square with the length of R.

样例输入

3 2
1 1
2 2
3 3

样例输出

3
 

提示

If two people stand in one place, they are embracing.


题意是对于n个点,长为r的正方形区域最多可以覆盖几个点(包括边界)。

总而言之就是暴力,枚举所有x坐标作为左边界,将能在正方形区域内的点存进数组,按y坐标排序,以其作为下边界,统计所有在范围内的点。

#include<bits/stdc++.h>
#define int long long

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
const int N=5010;
int n, m;
int a[N], b[N];
PII p[N];

signed main()
{
    // ios::sync_with_stdio(false);
    while(scanf("%lld%lld", &n, &m) != EOF) {
        for(int i = 0;i < n;i ++) {
            int x, y;
            scanf("%lld%lld", &x, &y);
            p[i] = {x, y};
            a[i] = x;
        }
        sort(a, a + n);
        int ans = 0;
        for(int i = 0;i < n;i ++) {
            int t = a[i];
            int idx = 0;
            for(int j = 0;j < n;j ++) {
                if(p[j].first <= t + m && p[j].first >= t) {
                    b[idx ++] = p[j].second;
                }    
            }

            sort(b, b + idx);
            int cnt = 0;
            for(int j = 0;j < idx;j ++) {
                while(cnt < idx && b[cnt] - b[j] <= m) cnt ++;
                ans = max(ans, cnt - j);
            }
        }

        printf("%d\n", ans);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值