Bear has a large, empty ground for him to build a home. He decides to build a row of houses, one after another, say in total.
The houses are designed with different height. Bear has workers in total, and the workers must work side by side. So at a time bear can choose some continuous houses, no more than , and add their heights by one, this takes one day to finish.
Given the designed height for each house, what is the minimum number of days after which all the houses’ heights are no less than the original design?
Standard Input
The first line of input contains a number , indicating the number of test cases. ()
For each case, the first line contains two integers and : the number of houses and the number of workers. The next line comes with non-negative numbers, they are the heights of the houses from left to right. (, each number will be less than )
Standard Output
For each case, output Case #i:
first. ( is the number of the test case, from to ). Then output the days when bear’s home can be built.
Samples
Input | Output |
Problem ID | 4 |
Problem Title | Complete Building the Houses |
Time Limit | 2000 ms |
Memory Limit | 64 MiB |
Output Limit | 64 MiB |
Source | The 11th UESTC Programming Contest Final |
这是一道贪心+差分的题目
贪心的思路是每次选择当前的最优策略,通过每一步的最优策略达到整体的最优。
特点是只考虑当下情况。
对于此题,既然每次最多能建的宽度是m,且建宽度1和建宽度m的花费是一样的,那么就一定每次建m。
现在考虑到通过最终的线性运算来实现一个区间的变换,所以用差分法。
看到洛谷上有一个评论:看到区间就想差分法。