Dave
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 3989 Accepted Submission(s): 1343
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 的正方形 然后问最多有多少个点会在这个正方形中
思路:
找出所有点中的最高的和最低的,然后 开始枚举 枚举x在里面的 然后取最值
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stdlib.h>
#include<map>
using namespace std;
#define inf 1<<30
#define N 1006
int n,r;
struct Node{
int x,y;
}p[N];
int yy[N];
int xx[N];
int main()
{
while(scanf("%d%d",&n,&r)==2)
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
yy[i]=p[i].y;
}
sort(yy,yy+n);
int ans=0;
for(int i=0;i<n;i++)
{
int xcnt=0;
for(int j=0;j<n;j++)
{
if(p[j].y<=yy[i]+r && p[j].y>=yy[i])
{
xx[xcnt++]=p[j].x;
}
}
sort(xx,xx+xcnt);
int e=0;
for(int j=0;j<xcnt;j++){
while(xx[e]<=xx[j]+r && e<xcnt) e++;
ans=max(ans,e-j);
}
}
printf("%d\n",ans);
}
return 0;
}