There is a square forest grid in which you wish to grow a forest. You have saplings (young trees) so that the -th sapling, once planted, will grow to a tree of height exactly metres tall. Each sapling has to be planted in a different square of the forest grid. A balanced forest is one in which for every tree in the forest at least one of the following conditions holds true:
* There exists a smaller tree in the same column.
* There exists a taller tree in the same row.
In how many ways can you grow a balanced forest?
Solution
The number of ways to grow a forest that may or may not be balanced is equal to . To answer the question we will first count the unbalanced forests, then subtract.
The main observation is that an unbalanced forest contains exactly one tree that violates both conditions. Indeed, if there were two such trees, say of heights and , then they cannot be in the same row or same column since . If tree is in square , then is the tallest tree in row and the smallest tree in column . Similarly, if tree is at position , then is the tallest tree in row and the smallest tree in column . Therefore, if the tree in square has size , then , hence . If the tree in square has size , then , a contradiction.
To plant an unbalanced forest, there are possible choices for the square with the tree that violates both conditions. The row and column that contain this square will together accommodate trees. There are possible choices for these trees. Once we have chosen numbers, the middle value (with smaller and larger numbers in the chosen set) has to become the tree that violates both conditions. The smaller trees can be planted in ways in their row and similarly for the larger trees.
The remaining trees can be planted as we like in the remaining squares of the grid. We have possibilities for that. In total we find that there are
ways to plant an unbalanced forest, hence the number of ways to plant a balanced forest is equal to