#P7416. [USACO21FEB] No Time to Dry P
[USACO21FEB] No Time to Dry P
Description
Bessie recently received a set of paints, and she wants to paint the fence at one end of her pasture. The fence consists of small segments, each meter long (). Bessie can use different colors, labeled from to from light to dark ( is very light, and is very dark). Thus, she can describe the desired color for each fence segment with an integer array of length .
At the beginning, all fence segments are unpainted. In one stroke, Bessie can paint any number of consecutive segments with the same color, as long as she does not paint a lighter color over a darker color (she may only use darker colors to cover lighter colors).
For example, an unpainted fence of length can be painted as follows:
0000 -> 1110 -> 1122 -> 1332
Unfortunately, Bessie does not have enough time to wait for the paint to dry. Therefore, she thinks she may need to give up painting some segments of the fence. Now, she is considering candidate intervals (). Each interval is given by two integers satisfying , representing the endpoints of the segments that need to be painted.
For each candidate interval, if all fence segments within the interval are painted to the desired colors and all segments outside the interval are left unpainted, what is the minimum number of strokes needed? Note that Bessie does not actually paint anything during this process, so the answer for each candidate interval is independent.
Input Format
The first line contains and .
The next line contains an integer array of length , representing the desired color of each fence segment.
The following lines each contain two space-separated integers and , representing a candidate interval to be painted.
Output Format
For each of the candidate intervals, output one line containing the answer.
8 4
1 2 2 1 1 2 3 2
4 6
3 6
1 6
5 8
2
3
3
3
Hint
Sample 1 Explanation
In this sample, painting the subarray with colors 1 1 2 requires strokes.
Painting the subarray with colors 2 1 1 2 requires strokes.
Painting the subarray with colors 1 2 2 1 1 2 requires strokes.
Painting the subarray with colors 1 2 3 2 requires strokes.
Subtask Properties
- For of the testdata, .
- For another of the testdata, .
- For another of the testdata, the input array does not contain numbers greater than .
- For the remaining of the testdata, there are no additional constraints.
Problem by: Andi Qu, Brian Dean, Benjamin Qi.
Translated by ChatGPT 5
京公网安备 11011102002149号