Question meaning:
Give you n points and a square with a side length of R. You can use this square to cover up to several points (points on the square boundary are also counted ).
Note: The side of a square must be parallel to the coordinate axis.
Ideas:
Sorts the Y coordinates of N points, and then O (n) enumerates the bottom boundary of a square. Take out the points of Y coordinate in the upper and lower boundary range of N points. Then, the O (n) complexity is used to find the points that can be overwritten by the side with the length of R. Therefore, the overall enumeration complexity is O (n ^ 2 ).
Code:
/* O (N ^ 2) Enumeration */# include <iostream> # include <cstdio> # include <algorithm> # include <memory. h> # define maxn 1005 # define INF 2e9 # define max (A, B) (A> B? A: B) # define min (a, B) (a <B? A: B) using namespace STD; struct node {int X, Y;} node [maxn]; int XX [maxn], YY [maxn]; int xCNT, ycnt; int N, R; void Init () {int I; for (I = 1; I <= N; I ++) {scanf ("% d ", & node [I]. x, & node [I]. y); YY [I] = node [I]. y;} Sort (yy + 1, YY + 1 + n);} void solve () {int I, j, E, ANS = 0; for (I = 1; I <= N; I ++) {xCNT = 0; For (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 ++] = inf; E = 0; fo R (j = 0; j <xcnt-1; j ++) {While (XX [e] <= XX [J] + r) e ++; ans = max (ANS, (e-j) ;}} printf ("% d \ n", ANS) ;}int main () {While (scanf ("% d ", & N, & R )! = EOF) {Init (); solve ();} return 0 ;}