Typical statistical questions
Question:
Returns the coordinates of N people and obtains the maximum number of people in the square with the edge of R.
Analysis:
Because the position of the square is not fixed and the most covered point must be satisfied, the Y coordinate of a certain point must be used as the bottom boundary to have the optimal solution. ALL:
First, sort the ordinate values of N people, enumerate all Y coordinates (the bottom boundary of a square), and find the points within the Square that satisfy the current y coordinate, enumerate the X coordinates of the points that meet the conditions, and calculate the points that can be overwritten with the R side. The total complexity is O (n ^ 2)
Code:
# Include <iostream> # include <algorithm> using namespace STD; struct node {int X, Y;} p [1001]; int N, ANS, R, XX [1001], YY [1001]; int main () {While (scanf ("% d", & N, & R) = 2) {for (INT I = 1; I <= N; I ++) {scanf ("% d", & P [I]. x, & P [I]. y); YY [I] = P [I]. y;} Sort (yy + 1, YY + 1 + n); ans = 0; For (Int J = 1; j <= N; j ++) // enumerate the bottom boundary {int CNT = 0; For (INT I = 1; I <= N; I ++) if (P [I]. y> = YY [J] & P [I]. Y <= YY [J] + r) xx [CNT ++] = P [I]. x; // locate all the points that meet the current bottom boundary, sort (XX, xx + CNT); XX [CNT] = int_max; // used to exit the while loop int e = 0; for (INT I = 0; I <CNT; I ++) // obtain the edge with the Left Border of the current point and the side length is R, points that can be overwritten at most {While (XX [e] <= XX [I] + r) e ++; ans = max (ANS, e-I );}} printf ("% d \ n", ANS);} return 0 ;}