Dave
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 3944 Accepted Submission(s): 1327
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
3HintIf two people stand in one place, they are embracing.
Source
Recommend
题意:给你N个点和一个边长为R的正方形,为你用这个正方形最多可以覆盖几个点(包括在正方形边界上的点)。
题解:N只有1000....
对y坐标的点排序,然后O(n)对N个坐标枚举这个正方形的上下界,然后取出y坐标在正方形内的点,并记录y坐标对应的x坐标。最后再对这些x坐标进行枚举,看是否在这个正方形中。
AC代码:
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int x,y;
}node[1005];
int xx[1005],yy[1005];
int xcnt,ycnt;
int N,R;
int main()
{
while(~scanf("%d%d",&N,&R))
{
for(int i=1;i<=N;i++)
{
scanf("%d%d", &node[i].x, &node[i].y);
yy[i]=node[i].y;
}
sort(yy+1,yy+1+N);
int temp,ans=0;
for(int i=1;i<=N;i++)
{
xcnt=0;
for(int j=1;j<=N;j++)
{
if(node[j].y >= yy[i] && node[j].y <= yy[i]+R)
xx[xcnt++]=node[j].x;
}
sort(xx,xx+xcnt);
xx[xcnt++]=2e9;
temp=0;
for(int j=0;j<xcnt-1;j++)
{
while( xx[temp] <= xx[j]+R ) temp++;
ans=max(ans,(temp-j));
}
}
printf("%d\n",ans);
}
return 0;
}