Description
Byteman, one of the most deserving employee of the goldmine of byteland, is about to retire by the end of the year. the Goldmine management wocould like to reward him in acknowledgment of his conscientious work. as a reward byteman may receive a small lot-a rectangular piece of the mine's area with sides of length S and W parallel to the axes of the coordinate system. he may choose the location of the lot. of course, a value of the lot depends on the location. the value of the lot is a number of gold nuggets in the area of the lot (if a nugget lies on the border of the lot, then it is in the area of the lot ). your task is to write a program which computes the maximal possible value of the lot (the value of the lot with the best location ). in order to simplify we assume that the terrain of the goldmine is boundless, but the area of gold nuggets occurrence is limited.
Taskwrite a program which:
1. reads the location of gold nuggets, 2. computes the maximal value of a lot (I. e. the maximal number of gold nuggets on the lot of given size), 3. writes the result. input
In the first line there are two positive integers S and W separated by a single space, (1 <= s, W <= 10 000 ); they denote the lengths of Lot's sides-parallel to the ox-axe and Oy-axe respectively. there is one positive integer n written in the second line, (1 <= n <= 15 000 ). it denotes the number of nuggets in the area of the goldmine. in the following n lines there are written the coordinates of the nuggets. each of these lines consists of two integers x and y, (-30 000 <= X, Y <= 30 000 ), separated by a single space and denoting the X and the Y coordinate of a nugget respectively.
Output
The out shoshould contain exactly one integer equal to the value of the most valuable lot of a given size.
Sample Input
1 2120 01 12 23 34 55 54 21 40 55 02 33 2
Sample output
4
Theme
Given N points on a two-dimensional plane, find a rectangle w * s (w s in height for a long time) so that it contains the most vertices.
The question in question B of the Shanghai Invitational competition is similar to that in question.
Ideas
Original black book question.
View each vertex as a scan of two events from left to right (the difference between the head and tail of the queue control team can not exceed the width, or be sorted in chronological order for processing ), each point is abstracted as a line segment with the height of the rectangle. The point with the most frequent line segments is the result.
In this way, O (N ^ 2) is reduced to O (N * log (a), and a is the range of point ordinate.
Here you can submit
Http://acm.cs.ecnu.edu.cn/problem.php? Problemid = 1350
PS: It's too weak. Not only did this problem come out on the spot, but it was still a day before it was done. The foundation still needs to be consolidated.
# Include <cstdio> # include <algorithm> # include <cstdlib> # include <cmath> # include <map> # include <vector> # include <cstring> using namespace STD; struct node {int T, POs, St;} ha [200000]; int x1, x2, V; int _ max; int N, W, H; int CMP (node X, node y) {return (X. T <Y. t); // sort by time sequence, delete the vertex and add another vertex} int maxv [400000], addv [400000]; void maintain (int o, int L, int R) {int lc = O <1; int rc = Lc + 1; maxv [O] = 0; If (r> L) {maxv [O] = max (maxv [LC], maxv [RC]);} maxv [O] + = addv [O];} void Update (int o, int L, int R) {int lc = O <1; int rc = Lc + 1; if (X1 <= L & X2> = r) addv [O] + = V; else {int M = L + (R-L)/2; If (X1 <= m) Update (LC, l, m); If (X2> m) Update (RC, m + 1, R);} maintain (O, L, R);} void query (int o, int L, int R, int add) {int lc = O <1; int rc = Lc + 1; if (X1 <= L & X2> = r) _ max = max (_ max, maxv [O] + Add); else {int M = L + (R-L)/2; If (X1 <= m) query (LC, L, M, addv [O] + Add); If (X2> M) query (RC, m + 1, R, addv [O] + Add) ;}} int main () {While (~ Scanf ("% d", & W, & H, & N) {memset (maxv, 0, sizeof (maxv); memset (addv, 0, sizeof (addv); _ max = 0; int o, p, Pt = 0; memset (HA, 0, sizeof (HA); For (INT I = 1; I <= N; I ++) {scanf ("% d", & O, & P); ha [++ pt]. T = O; ha [pt]. pos = P + 30001; ha [pt]. st = 1; ha [++ pt]. T = O + W + 1; ha [pt]. pos = P + 30001; ha [pt]. st =-1;} Sort (HA + 1, ha + Pt + 1, CMP); // sort all time points for (INT I = 1; I <= pt; I ++) {x1 = ha [I]. pos; x2 = ha [I]. pos + H; x2 = min (X2, 60001); V = ha [I]. st; u Pdate (60001,); // update point if (ha [I]. T! = Ha [I + 1]. T | I = pt) // output {x1 = 1; x2 = 60001; <span style = "white-space: pre "> </span> query (, 0); <span style =" white-space: pre "> </SPAN >}} printf (" % d \ n ", _ max );}}