CF1705 B, C, D1 Solutions
The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
B. Making Towers
Approach
Observe the figure provided for the first sample in the problem statement:

We can see that, if we want blocks of one color to form a tower, unless multiple blocks of the same color are adjacent in array and can be placed directly upward, we must place some blocks of other colors to both sides after placing a block of that color, then place blocks in the opposite direction, and finally make the two blocks of the same color lie on a straight line. It looks approximately like this:
1 | ⬆->->->A |
Here, represents a tower of one color, while the arrows represent the path along which colored blocks are placed.
Observation shows that an even number of blocks of other colors must be placed between two s. The explanation follows:
Suppose the first is at , and the number of blocks of other colors that we place to the right (it can also be to the left) is .
Then, to put the second at , colored blocks need to be placed at and . There are blocks in total, so the number is even (if blocks are stacked directly upward, it is , which is also even).
This means that, suppose there are two blocks of the same color, and , at positions and in array . Only when is odd can be stacked on top of , or on top of .
Moreover, can only be odd when and have different parity.
We can then solve the problem using DP. We repeat the same DP process for every color (in fact, it is more like a recurrence). Let be the greatest height of a tower that can be built using blocks of this color in array .
Then can transition from (with ), and, as described above, and should have different parity.
At the same time, we need to find the nearest block with different parity; otherwise, blocks may be wasted, or another block may be placed at a position that was already used earlier.
Code
1 | // author: tzyt |
C. Qpwoeirut And The City
Approach
We can see that, no matter what, there can be at most cool houses in the city.
If the number of houses is odd, only one arrangement can achieve this many cool houses. It is the arrangement shown by the first sample.

Beginning with the second house, make every house at an even position cool; that is, cool and non-cool houses appear alternately.
The cost of turning an ordinary house into a cool house can be calculated as follows:
1 | inline ll calc_cost(int i, int* h) { |
That is, make the current house one unit higher than the taller of its adjacent houses.
However, the case of an even number of houses is more complicated. In this case, must equal .
Then there are non-cool houses, so two adjacent non-cool houses must appear. These two consecutive non-cool houses can appear at any position, and we need to consider all cases.
For example, if , there are the following arrangements:
However, calculating every case from beginning to end would take too much time.
Therefore, we can calculate only the change in cost from one case to another.
For example:
During this process, the sixth house changes from cool to non-cool, while the seventh house changes from non-cool to cool.
Suppose we are currently changing house from cool to non-cool and house from non-cool to cool. We only need to call the preceding calc_cost, subtract the cost of , and then add the cost of .
1 | // author: tzyt |
D1. Chopping Carrots (Easy Version)
Approach
Let us try setting the minimum to . Then , because the minimum value of is , so is .
On this basis, we greedily try to make every as close to as possible. This makes the maximum as small as possible.
In this way, we can calculate . Because , we have . Of course, cannot be greater than , and if , we let .
We then enumerate every possible and calculate the maximum in that case to obtain the answer. The approach seems quite concise, but it really is difficult to think of.
Code
1 | // author: tzyt |





![[Stanford CS144] Lab 4 Record](/img/CS144/tcp%E7%8A%B6%E6%80%81%E6%B5%81%E8%BD%AC%E5%9B%BE.jpg)