Dave
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2768 Accepted Submission(s): 926
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).
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.
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
1 1
2 2
3 3
Sample Output
3
Hint
If two people stand in one place, they are embracing.
Source
AC代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
struct node{
int x,y;
}a[],b[];
bool cmp(node a,node b){
return a.x < b.x;
}
int main(){
int y[];
int n,r,i,j,Max;
int x1,x2,y1,y2;
int x_min,x_max,y_min,y_max;
while(EOF != scanf("%d%d",&n,&r)){
Max = -;
x_min = y_min = ;
x_max = y_max = -;
for(i=;i<n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
if(a[i].x < x_min)
x_min = a[i].x;
if(a[i].x > x_max)
x_max = a[i].x;
if(a[i].y < y_min)
y_min = a[i].y;
if(a[i].y > y_max)
y_max = a[i].y; b[i] = a[i];
}
if(y_max-y_min <= r && x_max-x_min <= r){
printf("%d\n",n);
continue;
}
sort(a,a+n,cmp);
int Max = ;
for(int i=;i<n;i++){
int k = ;
for(int j = i;a[j].x <= a[i].x + r && j < n;j++)//对x值不大于a[j].x + r遍历
y[k++] = a[j].y;//将比a[i] 的 x值小的a[j]点 的y值加入y数组
sort(y,y+k);//对y数组排序
int flag = ,temp = ;
for(int j = ;j < k && temp < k ;j++){//对y数组中所有元素遍历
while(y[temp] - y[j] <= r && temp < k)
temp++;
if(temp -j > Max)
Max= temp - j;
}
}
printf("%d\n",Max);
}
return ;
}