This is a SPOJ Problem. Here is the solution. It is for readers to optimize it further or give me better alternative means. Let’s take a look at the problem first.
Problem code: BGARDEN
Byteman owns the most beautiful garden in Bytetown. He planted n roses in his garden. Summer has come and the flowers have grown big and beautiful. Byteman has realized that he is not able to take care of all the roses on his own. He has decided to employ two gardeners to help him. He wants to select two rectangular areas, so that each of the gardeners will take care of the roses inside one area. The areas should be disjoint and each should contain exactly k roses. Byteman wants to make a fence surrounding the rectangular areas, but he is short of money, so he wants to use as little fence as possible. Your task is to help Byteman select the two rectangular areas.
The garden forms a rectangle l meters long and w meters wide. It is divided into l ·w squares of size 1 meter × 1 meter each. We fix a coordinate system with axes parallel to the sides of the garden. All squares have integer coordinates ( x,y) satisfying 1 ≤ x ≤ l, 1 ≤ y ≤ w. Each square may contain any number of roses.
The rectangular areas, which must be selected, should have their sides parallel to the sides of the garden and the squares in their corners should have integer coordinates. For 1 ≤l1 ≤l2 ≤l and 1 ≤w1 ≤w2 ≤w, a rectangular area with corners ( l1,w1), ( l1,w2), ( l2,w1) and ( l2,w2):
- contains all the squares with coordinates ( x,y) satisfying l1 ≤ x ≤ l2 and w1 ≤ y ≤ w2, and
- has perimeter 2 ·( l2 – l1 +1)+2 ·(w2-w1 +1).
The two rectangular areas must be disjoint, that is they cannot contain a common square. Even if they have a common side, or part of it, they must be surrounded by separate fences.
Task
Write a program, that:
- reads from the standard input the dimensions of the garden, the number of roses in the garden, the number of roses that should be in each of the rectangular areas, and the positions of the roses,
- finds the corners of two such rectangular areas with minimum sum of perimeters that satisfy the given conditions,
- writes to the standard output the minimum sum of perimeters of two non-overlapping rectangular areas, each containing exactly the given number of roses (or a single word NO, if no such pair of areas exists).
Input
The first line of standard input contains two integers: l and w (1 ≤ l,w ≤ 250 ) separated by a single space — the length and the width of the garden. The second line contains two integers: n and k (2 ≤ n ≤ 5000 , 1 ≤ k ≤ n/2 ) separated by a single space — the number of roses in the garden and the number of roses that should be in each of the rectangular areas. The following n lines contain the coordinates of the roses, one rose per line. The ( i+2)nd line contains two integers li, wi (1 ≤ li ≤ l, 1 ≤ wi ≤ w) separated by a single space — the coordinates of the square containing the ith rose. Two or more roses can occur in the same square.
In 50% of test cases, the dimensions of the garden will satisfy l,w ≤ 40 .
Output
The standard output should contain only one line with exactly one integer — the minimum sum of perimeters of two non-overlapping rectangular areas, each containing exactly k roses, or a single word NO, if no such pair of areas exists.
Example

For the input data:
6 5
7 3
3 4
3 3
6 1
1 1
5 5
5 5
3 1
the correct result is:
22
1 2 3 4 5 6
1
2
3
4
5
| Added by: |
Ngô Minh Đức |
| Date: |
2008-12-18 |
| Time limit: |
0.5s |
| Source limit: |
50000B |
| Languages: |
All except: C++ 4.3.2 TCL SCALA PYTH 2.6.2 ERL TECS JS |
| Resource: |
IOI 2005 – Poland |
____________________________________________________________
Solutions:
All through the solution I have used the array notation rather than the Cartesian co-ordinate convention of the problem statement.
ALGORITHM 1A
The first thing that comes to mind is to apply brute force to find the minimum perimeter by checking all the (w*l)*(w*l) rectangles (i.e. we can find top left corner in w*l ways and bottom right corner in w*l ways) and to count the number of roses in each of those rectangles it takes time proportional to further w*l.
thereby taking a total time of O(w*w*w*l*l*l)……………………. (1.1)
we can find the least among them in O(w*w*l*l)………………….(1.2)
thereby effective time= w*w*w*l*l*l + w*w*l*l .
i.e. O(w*w*w*l*l*l)…………………………………………………………..(1.3)
ALGORITHM 1B
Are we done? We have just calculated minimum perimeter of a rectangle containing exactly k roses.
But in the problem statement we are asked to find two disjoint (non-overlapping) rectangles having exactly k roses such that their sum of perimeters is least. So didn’t we just goof up? Didn’t we end up calculating something not required. Let’s now proceed towards what we think is required for the given problem.
So among w*w*l*l rectangles we can find the two rectangles having exactly k roses each with least sum of perimeters among all non-intersecting pair of rectangles in
O((w*w*l*l)*(w*w*l*l))…..…………………………………….. (1.4)
(i.e. selection of two objects among n objects in done in nC2 ways i.e.O(n*n),here the number of objects is w*l*w*l rectangles)
thereby effective time= w*w*w*l*l*l + w*w*w*w*l*l*l*l .
i.e. O(w*w*w*w*l*l*l*l)…………………………………………………. (1.5)
This makes a horribly poor time complexity.
ALGORITHM 2
Careful observations can lead to a much better result.
Firstly we don’t need to count the number of roses in a given rectangle traversing through the entire rectangle every single time only if we pre compute it dynamically.
If R(r1,c1,r2,c2) — the number of roses in a rectangular region with corners (r1,c1) and (r2,c2) is:
R(r1,c1,r2,c2) = R(r2,c2) − R(r2,c1−1)− R(r1−1,y2) + R(r1−1,c1−1) …………………………………………(2.1)
The pre-computation matrix form the above recurrence relation is dynamically generated in O(w*l )…………………………………………….(2.2).
Thus time for rose counting operation for a rectangle reduces down from O(w*l) to O(1) ……………………………………………..(2.3)
and time for counting for all (w*l)*(w*l) rectangles reduces from O(w*w*w*l*l*l) to O(w*w*l*l)……………………………………….(2.4)
effective time= (w*l+w*w*l*l) + w*w*w*w*l*l*l*l .
i.e. O(w*w*w*w*l*l*l*l)…………………………………………………..(2.5)
However the above trick of introducing dynamic programming does not reduce our time as the time complexity of the algorithm is dominated by the last term(i.e. finding of two rectangles with least sum of perimeters among all non-intersecting pair of rectangles)
ALGORITHM 3
This can make the improvement in ALGORITHM 2 look useless as it eventually results in the same time complexity as ALGORITHM 1B.To make our effort of trickily implementing dynamic programming to compute the number of roses in a given rectangle not go in vain we need to reduce the time of finding of two rectangles with least sum of perimeters among all non-intersecting pair of rectangles too.
The first part of observation is if the rectangles are non-overlapping then there must be a line separating them(either horizontal or vertical).So we are no longer required to find disjoint pair of rectangle among (w*l)*(w*l) rectangles. We now just have to find least perimeter rectangle containing exactly k roses on either side of this line and then add the two. We have to do this for all l-1+w-1 such lines and find which of these l-1+w-1 cases yields a minimum.
So finding rectangle of least perimeter containing k roses algorithm from ALGORITHM 1A and counting of roses in a given rectangle from ALGORITHM 2 we have,
effective time= (w*l+w*l*w*l) + w*w*l*l*(w+l).
i.e. O(w*w*l*l*(w+l))………………………………………………………..(3.1)
Still the 2nd part of the time complexity equation dominates but we have reduced it significantly from O(w*w*w*l*l*l) to O(w*w*l*l*(w+l))……………………………………………………………..(3.2)
<<Note:-did you notice how finding of rectangle having the least perimeter with k roses comes into play. when we initially did that in ALGORITHM 1A it looked so irrelevant but now the more formidable idea of considering two rectangles with least sum of perimeters among all non-intersecting pair of rectangles is no longer being considered a good idea>>
ALGORITHM 4
Every time we are selecting a line we are having to check for all possible rectangles on either side of the line cant we optimize it any further. The answer is yes we can look whenever we are shifting a line to its next line we are checking a large number of rectangles many of which are same as that we found for the last line. So don’t you think we are unnecessarily rechecking them? Here again to avoid re-computation we can take the help of dynamic programming. Let’s see how. I wont call it dynamic programming as we are not having any recurrence relation to work with but we know there are something common for two or more line and that re-computation needs to be avoided. So we need some sort of relation.
Observe carefully, say we have a line next to r, let’s consider the lower part of r. So we have to find all possible rectangles whose one line(take it to be upper line) must be lying among one of the lines r+1, r+2,….,l.
If we define MD(r) as the Minimum Perimeter rectangle of k rose Down of Row r and MSR(ri) as the Minimum Perimeter rectangle of k rose with the Starting side exactly along ith Row.()
Then we have MD(r) = min {MSR(r+1), MSR(r+2)…, MSR(l)}….(4.1)
Similarly we can have MU, MR, ML and analogously MER,MSC,MEC
Now let’s discuss how to make use of such a relation. For that we use a kind of memorization. Lets set the 4 lists MSR,MER,MSC and MEC initially to infinity. Now we just traverse once (i.e. consider all w*l*w*l rectangles) and update these 4 lists accordingly. This operation takes only time of O(w*l*w*l)…………………………………………………(4.2)
For a given line we can get the least perimeter k rose containing rectangle from its respective list just in linear time i.e. O(l) or O(w)…………(4.3)
Considering all lines effective time is O(l*l+w*w)…………………(4.4)
After incorporation of this idea of memorization into our algorithm
effective time = (w*l+w*w*l*l) + {w*w*l*l +( l*l +w*w)}
i.e. O(w*w*l*l)………………………………………………………(4.5)
ALGORITHM 5
Now here is a point worth noting. Interestingly if we see very carefully observe we can find that we do not need to consider all rectangles having exactly k roses.
We can restrict our considerations to only those rectangles having exactly k roses which do not contain any other rectangles having exactly K roses in their interiors. So here we incorporate our 4th trick.
Let us consider a case for a given r1 and r2.Say suppose we have found
Case1:R(r1, c1, r2, c2) =k then we have found a required rectangle and we are sure that for minimum perimeter and for a given r1 and r2 the value c2-c1 can not be any greater but it may not be lesser. So there is no point in incrementing c2 for same c1.so we increment c1 (trying to find smaller rectangles with k roses) keeping
Case2:R(r1, c1, r2, c2) >k then is a chance of finding a rectangle in its interior with exactly k rosés. We contract the rectangle by incrementing c1 (why did not we contract by decreasing c2? because that case has already been considered)
Case3:R(r1, c1, r2, c2) <k then there is no point decreasing c2-c1 value further because if this rectangle does not have k roses then there is no way a rectangle interior to it will have k roses we increase c2 to make the rectangle bigger.
In this entire algorithm we either increment c1 or c2 until c2>w
So it takes a linear time i.e. O(w)…………………………………(5.1)
But remind u, we have done this calculation for a given r1 and r2.
Now we have to consider all pair of r1 and r2 which takes O(l*l)…………..(5.2)
As for a given rectangle number of roses can be found in O(1) [from equation 2.2]time we are not required to check no of roses for all possible w*w*l*l. We will only retrieve the number of rose information in O(1) time and that time will be proportional to the number of rectangles taken up for consideration i.e.O(w*l*l)…………………………………………………………(5.3)
Effective time = (w*l+w*l*l) + {w*l*l + (l*l +w*w)} i.e.O(w*l*l)………………………………………………………..(5.4)
<<note: in both case1 and we increment c1 the only difference is in case1 we update the memorization list values i.e. MPSC(c1) , MPEC(c2), MPSR(r1) and MPER(r2) before shifting the starting column c1 rightward>>
Questions that should come to mind:
- Why are we having to count a single rose every time for all w*l*w*l rectangles?
- How have we utilized the fact that the two rectangles will be disjoint?
- Why do we have to have to traverse through entire side of a line just for altering the line one place to the right or left we must prevent recalculation of those rectangles already found. Don’t we?
- What about those rectangles of k roses within another rectangle of k roses. They both must have the same k roses which all must lie in the interior most rectangle of k roses. Then why are we calculating. The innermost must have the minimum perimeter. Isn’t it?
Their answers are the following tricks that we applied:
- Dynamic Programming to pre-compute number of roses.
- Separating Lines concept
- Memorization List
- Contraction & Expansion of rectangle along a particular axis with size constraint.
22.605194
88.433901