Editorial for COCI '08 Contest 4 #6 Periodni
Submitting an official solution before solving the problem yourself is a bannable offence.
The table in the problem is called a histogram. We need to count the number of ways to separate the gases into cells in the histogram , according to the rules outlined in the problem statement. Let that number be . We can calculate it recursively.
Find the height of the lowest column in and call it .
There are three cases:
All columns are of height – the histogram is empty. Therefore:
. There is a column of height zero, which means that we can split the histogram into two sub-histograms and (left and right of the zero-height column). We can independently calculate the number of ways to put gases into the two histograms, combining the results:
. There is a rectangle of height that is as wide as the histogram (call this width ). We will solve this case by counting how many ways we can put gases into rectangle , the remaining gases going into the sub-histogram (which is without ). We can choose the rows into which to put gases in ways and permute them in ways. Then we can choose the columns in ways, because columns are already occupied by gases above, in sub-histogram .
The total number of different histograms that will occur as the parameter in the recursion is in linear proportion to the width of the histogram: a recursive call in case 3 always yields a case 2 histogram, which reduced the histogram in width.
For a full score, it is also required to efficiently calculate the binomial coefficients.
Comments