Bookshelf题解动态规划DP

来源:互联网 发布:高效程序员的45个习惯 编辑:程序博客网 时间:2024/05/18 22:13

Description

Robby bought a new bookshelf, and he wanted to put all his N books on it. The bookshelf contains K layers, and the heights of each layer are variable. That is, the minimum height of one layer is equal to the maximum height of all the books in this layer. And the height of the bookshelf is the sum of all layers' heights.Now Robby wants to make the height of his bookshelf as low as possible, but he don't want to break the order of the books, that is, each layer must contain the consecutive books of the original book series. And each layer must contain at least one book.

Input

There are several test cases in the input. The first line of each test case contains two integers N and K (1 ≤ K ≤ N ≤ 100). The second line contains N integers, indicating the heights of the books. You can assume the height of every book is no more than 1000.The input is terminated with N = K = 0.

Output

Output one line for each test case, that is, the minimum height of the bookshelf.

Sample Input

4 21 4 2 34 24 1 2 34 41 2 3 40 0

Sample Output

5710

Hint

For the first sample, the best strategy is 1+max(4,2,3)=5For the second sample, the best strategy is max(4,1,2)+3=7For the third sample, the best strategy is 1+2+3+4=10

Source

TJU Programming Contest 2007 by RoBa
有序的划分模型
关键是要确定第i本书要放到那层书架上。
另dp[i][j]为第i本书放到第j层书架上时的总高度的最小值。
则dp[n][k]即为所求。
dp[i][0]=INF
对于第i本书,能放到的书架层数x是有个范围的。
对于第x层书架,能放进去的书也是有个范围的。
dp[i][j]=min{dp[k-1][j-1]+M[k][i],k>=j&&k<=i} 
①单独放到j层书架上。
②第k本书到第i本书放到第j层书架上。
取最小值
其中M[k][i]需要预处理出来第k本书到第i本书之间(包含)的最大书本高度。
因此算法复杂度为O(n^3).
 
原创粉丝点击