2011年大连ACM网络赛 hdu 4007 Dave

Dave

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 387    Accepted Submission(s): 107


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
 

Statistic |  Submit |  Discuss |  Note
题意:在平面内寻找正方形可以圈住的点的最大的个数

分析:先固定坐标x(升序排序),在找点P[i].x~P[i].x+r范围内的点的y坐标的Y[i],逐点(沿x方向)向后推移,查找
蛋疼的是:这个题竟然不包含矩形倾斜的情况!

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define N 1010

struct Point
{
    int x,y;
}P[N];

int Y[N],J;
int n,r;

bool cmp(Point a,Point b)
{
     return a.x<a.x;
}

void getY(int x1,int x2)//得到P[i].x-P[i].x+r的点的y坐标
{
    int i;
    J=0;
    for(i=0;i<n;i++)
    {
        if(P[i].x>=x1&&P[i].x<=x2)
        Y[J++]=P[i].y;
    }
    sort(Y,Y+J);
}

int how()//查找当前Y[]中的矩形可圈住的点的个数最大值
{
    int ans=0;
    int ans1=0;
    int i,j,k;
    int low=0;
    for(i=0;i<J;i++)
    {
        while(Y[i]-Y[low]>r)low++;//设置两个指针标记i,low
        ans=i+1-low;
        if(ans1<ans)ans1=ans;
    }
    return ans1;
}
int main()
{
    int i,j,k,ans,ans1;
    while(scanf("%d%d",&n,&r)!=EOF)
    {
        ans1=ans=0;
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&P[i].x,&P[i].y);
        }
        sort(P,P+n,cmp);
        int tt = -1;
        for(i=0;i<n;i++)
        {
            if(P[i].x == tt) continue;
            tt = P[i].x;
            getY(P[i].x,P[i].x+r);
            ans1=how();
            if(ans<ans1)ans=ans1;
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
6 2
1 1
1 3
2 1
2 3
3 1
3 3
*/


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值