https://mirror.codeforces.com/contest/1269/problem/D
In editorial it is given that answer is min(black colors , white colors) .But why it cannot be less than that ? Also what is the problem with algorithm when histogram is not given in sorted order (i.e decreasing height) ?
Also one request to editorial writers . You guys are doing great job,thanks, but if you write more simple explanation it will help beginners and people in general a lot ! (I am not saying that editorial of this contest is not good).
Editorial showed a algorithm to build $$$min(\text{black cells}, \text{white cells})$$$ dominos. The proof for the algorithm uses the fact that the histogram is sorted, so two equal rows are always adjacent.
Without affecting the answer, we add
'0'
at the end of the histogram.We define two operations:
Example:
8 7 3 1 -> 8 7 1 1 -> 8 1 1 1 -> 2 1 1 1
Example:
3 2 2 1 -> 3 1 1 1 -> 3 1 0 0
Repeatedly do the operation (1) and (2) until you can't. Note if no neighbour difference $$$ \geq 2 $$$ or $$$ = 0 $$$, then all neighbour difference $$$ = 1 $$$, which is "basic diagram" stated at the editorial. We then can just choose all dominos vertically on the "basic diagram".