#P4909. [USACO06MAR] Ski Lift G

[USACO06MAR] Ski Lift G

Description

The mountains of Colorado are represented as a polyline in the 2D plane. This polyline has NN endpoints and N1N - 1 segments. The ii-th endpoint has x-coordinate ii and y-coordinate HiH_i. The y-coordinate represents altitude (elevation).

Ron plans to build a ski resort for the cows, and needs to plan a ski lift line along the mountains. The cable line is also a polyline, composed of several cable segments, starting at the first mountain endpoint and ending at the last endpoint. Each cable segment can either follow the outline of the mountain or be suspended in the air, skipping several endpoints with lower altitude. The horizontal span of each cable segment is limited and cannot exceed the given integer KK. Ron must build pillars at the endpoints of each cable segment to fasten the cable.

Please help him plan which mountain endpoints should have pillars so that the number of pillars is minimized. Note that, by the problem statement, the start and end endpoints must have pillars.

Constraints 2N50002 \le N \le 5000, 1KN11 \le K \le N - 1, 0Hi1090 \le H_i \le 10^9.

Input Format

The first line contains two integers NN and KK.

Lines 22 through N+1N + 1: line i+1i + 1 contains an integer HiH_i.

Output Format

Output a single integer, the minimum number of pillars required.

13 4
0
1
0
2
4
6
8
6
8
8
9
11
12
5

Hint

Explanation: The optimal plan is to place pillars at 1,5,7,9,131, 5, 7, 9, 13. 55 cannot directly connect to 99 because the altitude at 99 is higher; 11 cannot directly connect to 77 because the span exceeds KK.

Translated by ChatGPT 5