Editorial for Baltic OI '08 P5 - Grid


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Subtask 1

The simplest solution is to consider all (nr)(ms) placings of lines.

Time Complexity: O((nr)(ms)nm)

Subtask 2

If we fix the placing of lines going in one direction, then the optimal placing of lines going in the other direction can be computed much faster. There are two possible approaches:

  • We can use binary search to find an optimal computing time. Given a candidate for an optimal computing time and placing of all horizontal lines, we can place the vertical lines from the leftmost to the rightmost one using greedy approach: each vertical line should be put as far to the right as possible. This solution runs in O((nr)nmlogS) time, where S is the sum of all computation times for individual squares.

  • Another approach considers all (nr) placings of horizontal lines. For each placing, it calculates the minimal processing time using dynamic programming. Let min[i][j] be the minimal processing time of i first columns divided with j meridians. The array min can be easily filled in O(m2n) time. The overall running time is O((nr)m2n).

Time Complexity: O((nr)nmlogS) or O((nr)m2n)


Comments

There are no comments at the moment.