Editorial for Balkan OI '11 P6 - Trapezoids
Submitting an official solution before solving the problem yourself is a bannable offence.
This is a dynamic programming task, with the fruitful use of well-known data structures. There are various possible approaches with time and memory complexities , and .
Every trapezoid is represented by two intervals (one on each horizontal line). We can normalize the coordinates, and get the permutation from to . This can be easily done by sorting the arrays and (and and ) in time, with two additional arrays.
We could make things easier by introducing two dummy trapezoids, one on the left and one on the right side with large coordinates.
Next, we will calculate the size of the maximum independent set. We can define partial ordering by sorting the trapezoids according to the upper left corner . Let denote the size of the maximum independent set of trapezoids that contain trapezoid . It simply follows:
, where and lies completely on the left of
This produces an dynamic programming algorithm.
In order to get an solution, we will use a binary indexed tree (cumulative table) data structure. Cumulative tables in logarithmic time perform three operations on the array :
- for given index and number , add to the value .
- for given index , calculate the partial sum .
- for given index , calculate the maximum value among .
Let be the cumulative table for maintaining the partial maximums. Traverse the coordinates from to , and for each left upper coordinate , calculate based on the maximum value among , while for the right upper coordinate , write in the cumulative table on the index . The final solution is .
The second part is more difficult. The quadratic dynamic programming solution is given in the following pseudocode:
num_max_ind[0] = 1;
for i = 1 to n do begin
num_max_ind[i] = 0;
for j = 0 to i - 1 do begin
if ((max_ind[j] + 1 == max_ind[i]) and (a[j] > b[i]) and (c[j] > d[i]) then
num_max_ind[i] = num_max_ind[i] + num_max_ind[j];
end;
end;
We will use again cumulative tables for designing an solution. The main problem here is how to manipulate the partial sums. Namely, for each trapezoid , we need to sum the values of those trapezoids which are entirely on the left of with additional condition .
This can be done by considering pairs of two neighboring values . Using two additional arrays for coordinates and trapezoid indices, we can traverse trapezoids from left to right, such that or . First, we need to carefully preprocess all trapezoids and store the coordinates for each pair in one array (or array of dynamic arrays) in order to keep memory limit . Therefore, we fill the cumulative table with the values for trapezoids with , and calculate the value for trapezoids with . Instead of resetting the cumulative table for each , we can remove the values by traversing the array of indices again.
Since every trapezoid will be added and removed from the cumulative table, the total time complexity is . This reusing of the cumulative table makes this task very interesting.
Comments