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).
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
AC代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <math.h> 5 using namespace std; 6 struct node{ 7 int x,y; 8 }a[1010],b[1010]; 9 bool cmp(node a,node b){ 10 return a.x < b.x; 11 } 12 int main(){ 13 int y[1010]; 14 int n,r,i,j,Max; 15 int x1,x2,y1,y2; 16 int x_min,x_max,y_min,y_max; 17 while(EOF != scanf("%d%d",&n,&r)){ 18 Max = -1; 19 x_min = y_min = 999999999; 20 x_max = y_max = -1; 21 for(i=0;i<n;i++){ 22 scanf("%d%d",&a[i].x,&a[i].y); 23 if(a[i].x < x_min) 24 x_min = a[i].x; 25 if(a[i].x > x_max) 26 x_max = a[i].x; 27 if(a[i].y < y_min) 28 y_min = a[i].y; 29 if(a[i].y > y_max) 30 y_max = a[i].y; 31 32 b[i] = a[i]; 33 } 34 if(y_max-y_min <= r && x_max-x_min <= r){ 35 printf("%d ",n); 36 continue; 37 } 38 sort(a,a+n,cmp); 39 int Max = 0; 40 for(int i=0;i<n;i++){ 41 int k = 0; 42 for(int j = i;a[j].x <= a[i].x + r && j < n;j++)//对x值不大于a[j].x + r遍历 43 y[k++] = a[j].y;//将比a[i] 的 x值小的a[j]点 的y值加入y数组 44 sort(y,y+k);//对y数组排序 45 int flag = 0,temp = 0; 46 for(int j = 0;j < k && temp < k ;j++){//对y数组中所有元素遍历 47 while(y[temp] - y[j] <= r && temp < k) 48 temp++; 49 if(temp -j > Max) 50 Max= temp - j; 51 } 52 } 53 printf("%d ",Max); 54 } 55 return 0; 56 }