HDU 4007:Dave (枚举)

来源:互联网 发布:java服务器插件 编辑:程序博客网 时间:2024/06/05 01:04

Dave

Time limit:1000 ms Memory limit:65768 kB


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


题意:

现在给出n个点的坐标,然后要我们画一个边长为R的正方形,使得这个正方形中的点最多。

解题思路:

枚举每个点的y坐标,作为上界,那么y+R就是其下界,在其中统计在这个范围中的x,排序后,另一个循环枚举x的左右边界。复杂度应该是O(N^2logN)。

Code:

#include <iostream>#include <cmath>#include <cstdio>#include <algorithm>#include <cstring>#include <stdlib.h>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;typedef long long LL;const int INF=3e9;const int maxn=1005;struct Node{    int x,y;}p[maxn];bool cmp(Node a,Node b){    return a.y<b.y;}int xx[maxn];int main(){    int n,r;    while(scanf("%d%d",&n,&r)!=EOF)    {        for(int i=0;i<n;i++)        {            scanf("%d%d",&p[i].x,&p[i].y);        }        int ans=0;        sort(p,p+n,cmp);        for(int i=0;i<n;i++)        {            int L=p[i].y,R=p[i].y+r;            int len=0;            for(int j=0;j<n;j++)            {                if(p[j].y>=L&&p[j].y<=R)                {                    xx[len++]=p[j].x;                }            }            sort(xx,xx+len);            xx[len++]=INF;            int temp=0;            for(int j=0;j<len-1;j++)            {                while(xx[temp]<=xx[j]+r)                    temp++;                ans=max(temp-j,ans);            }        }        printf("%d\n",ans);    }    return 0;}
原创粉丝点击