#P3287. Maximizing a Beautiful Corn Row
Maximizing a Beautiful Corn Row
Maximizing a Beautiful Corn Row
Uncle Fang is taking a stroll by his cornfield when he notices a row of N corn plants of varying heights. He believes that a non‐decreasing sequence in plant heights is beautiful. To achieve this, he is allowed two types of operations:
- Boost operation: Select a contiguous interval of corn plants and increase each plant's height by 1 unit. He can perform at most K such operations.
- Removal operation: Remove any set of corn plants arbitrarily.
After performing the boost operations and removing some plants, the remaining corn plants (in their original order) should form a non‐decreasing sequence. What is the maximum number of corn plants that can remain in a beautiful row?
Note: If the original heights are denoted by \(h_1,h_2,\dots,h_N\), and you choose a subsequence with indices \(i_1 < i_2 < \dots < i_m\), then after applying boost operations the height at position \(i\) becomes \(h_{i}+x_i\) (where \(x_i\) is the total boost applied to that corn). Since each boost operation increases a contiguous block by 1 unit, the boost sequence \(x_1,x_2,\dots,x_N\) is such that the sum of all positive jumps in boost values (i.e. \(x_{i+1}-x_i\) when positive) is at most \(K\). To form a non-decreasing sequence you must have:
[ h_{i_j}+x_{i_j} \le h_{i_{j+1}}+x_{i_{j+1}} \quad \text{for all } 1 \le j < m. ]
This condition implies that for every adjacent pair in your chosen subsequence, you need to allocate extra boosts of at least \(\max(0, h_{i_j}-h_{i_{j+1]})\) across the sequence, and the total extra boost used must not exceed \(K\). Your task is to determine the subsequence with the maximum length that can be "fixed" with at most \(K\) boost operations.
inputFormat
The first line contains two integers \(N\) and \(K\) where \(N\) is the number of corn plants and \(K\) is the maximum number of boost operations allowed.
The second line contains \(N\) integers \(h_1, h_2, \dots, h_N\) representing the height of each corn plant.
outputFormat
Output a single integer, the maximum number of corn plants that can remain (i.e. the length of the longest subsequence) forming a non-decreasing sequence after applying the operations.
sample
5 1
4 1 2 3 1
3
</p>