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
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
题意:首先给你几个点,再给你个正方形边长,让你求出在以该边长为正方形最多能包含多少点。
思路:一开始听himdd学长讲了思路,说是用到了离散数学。。。很是纠结,,,不过上网看了看别人代码,大都是用枚举的方法,感觉挺不错的。先把所有点的纵坐标从小到大排序,然后在以每一点纵坐标为下边界,以该坐标+r为上边界,找出其所对应的横坐标,并记录,然后在把横坐标排序,以每一点的横坐标为左边界以该点的横坐标+r为右边界枚举,从而得出该正方形的可包含的最多的点。
代码:
#include<iostream> #include<algorithm> using namespace std; #define N 1005 typedef struct{ int x,y; }point; int n,r; point map[N]; int xx[N],yy[N]; int main() { while(cin>>n>>r) { for(int i=1;i<=n;i++) {cin>>map[i].x>>map[i].y; yy[i]=map[i].y; } sort(yy+1,yy+n+1); int ans=0; for(int j=1;j<=n;j++) { int cnt=0; for(int i=1;i<=n;i++) if(map[i].y>=yy[j]&&map[i].y<=yy[j]+r) xx[cnt++]=map[i].x; sort(xx,xx+cnt); xx[cnt]=INT_MAX;//为了跳出下面的while循环。 for(int i=0;i<cnt;i++) { int e=i; while(xx[e]-xx[i]<=r) e++; ans=max(ans,e-i); } } cout<<ans<<endl; }return 0; }