Editorial for Baltic OI '05 P1 - Camouflaged camp
Submitting an official solution before solving the problem yourself is a bannable offence.
Actually, there is nothing to spoil in this task. It is perhaps the only problem that could be solved by the guys from the military academy. The only possible solution is already told in the task. Every location on the map should be checked for almost every characteristic. Actually, a good optimization comes from that "almost every characteristic", it will be discussed later.
It is easy to estimate that there is no way to calculate characteristics at every location of the map in time, by simply summing the values inside every characteristic. However, it is possible to calculate every characteristic a little bit faster – in constant time. Consider the following green characteristic anywhere on the map.
There are six rectangles in the picture. Every rectangle starts at the top left corner of the map and continues to the corner with a red number – rectangle id. The altitude map can be converted to the map of summed values in all possible rectangles of this type. The converting can be done in linear time. Furthermore, a sum of values in any rectangle (of any type) on the map can be calculated in a constant time. For example, sum of values in a dark green rectangle can be calculated by , where means the sum of values in precalculated rectangle . Similar, in light green rectangle – by .
To calculate a characteristic, the average value of altitude in the green rectangles should be compared. The comparison can always be converted to the difference that is compared to zero, in the example:
That's how the characteristics can be calculated in a constant time.
One more optimization that was mentioned earlier is possible. We are looking for the location where maximum altitude requirements of the characteristics are met. We can skip checking the characteristics at every location, if number of the characteristics that are left to check at that location summed with the number of already calculated characteristics that met the altitude requirement cannot exceed the maximum number of good characteristics found till now.
Comments