#C11815. Minimum Painting Operations on a Fence
Minimum Painting Operations on a Fence
Minimum Painting Operations on a Fence
You are given a fence which is divided into N consecutive sections. Each section is painted with a color represented by an integer (its height). In a single painting operation, you can repaint a contiguous segment of the fence with a new color. However, if two connected sections already share the same color, they can be painted together with one operation.
Your task is to determine the minimum number of painting operations required to cover the entire fence.
Note: If the fence is empty (i.e., N = 0), no operations are needed.
The answer is effectively the number of contiguous segments of identical color. In other words, if the fence heights are represented by the sequence \(h_1, h_2, ..., h_N\), the result is \(1 + \sum_{i=2}^{N} \mathbf{1}_{\{h_i \neq h_{i-1}\}}\) for \(N > 0\), and 0 for \(N = 0\).
inputFormat
The first line of input contains an integer N, representing the number of sections in the fence.
The second line contains N space-separated integers, where each integer represents the color (or height) of a fence section.
outputFormat
Output a single integer: the minimum number of painting operations required to paint the entire fence.
## sample5
2 2 1 2 2
3