Time limit for organizing books:MS | Memory limit:65535 KB Difficulty:5
-
Describe
-
Xiao Ming is a book Stork ape, he has a lot of books piled together on the shelves, each stack of books are placed horizontally, and each stack of books is ordered is a whole, not separate, (can imagine the shelf is a straight line), but these books height is uneven, Xiao Ming has obsessive-compulsive disorder, Look had to be neat so he wanted to make these books the height of a non-descending sequence he was comfortable, but these books are orderly, so he can only one of the stacks of books and his next book bound together to form a pile of new books, then he minimum number of bookbinding?
-
-
Input
-
-
multiple sets of test data, processing to end of file
Each set of data starts with an n (1<=n<=1000) representing n stacks of books
The next line is the height of this n stack of books A[i], (1<=a<=10^5) (though this height is a bit of a rip)
-
-
Output
-
-
first output case num: Indicates the group of data
Next, the minimum number of binding times for each set of data output
-
-
Sample input
-
-
58 2 7) 3 11100
-
-
Sample output
-
-
Case 1:3case 2:0
-
-
Tips
-
-
first set of examples: The following 4 books bound together, binding 3 times, composed of 8 13
The second set of examples: only one book, no binding
-
-
-
-
-
How to solve the problem: we define dp[i] as the minimum number of steps to organize the first stacks of books into non-descending sequences. Definition H[i] represents the highest book height of the stack of books before I. At the same time Sum[i] to record the total height of the first stack of books. dp[i]=dp[j]+ (i-j-1). If the highest book in the front J stacks is less than or equal to sum[i]-sum[j]. Then determine whether J--->i so many stacks of books merged together is smaller than dp[i], and if so, update dp[i],h[i]. Why is it that the best results are even if we can find the update dp[i] from i-1? Because Dp[j] is the optimal result, then I find the smallest increment based on the optimal solution, then dp[i] is definitely the optimal solution.
-
-
#include <bits/stdc++.h>using namespace Std;const int INF = 0x3f3f3f3f;const int maxn = 5050;int A[MAXN], SUM[MAXN], DP[MAXN], H[maxn];int main () {int n, cnt = 0; while (scanf ("%d", &n)! = EOF) {memset (sum, 0, sizeof (sum)); for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]); H[i] = max (h[i-1], a[i]); Sum[i] = Sum[i-1] + a[i]; } memset (DP, INF, sizeof (DP)); Dp[0] = 0; The current 0 or 1 stack book non-descending requires a minimum binding number of 0 dp[1] = 0; for (int i = 2, I <= N; i++) {for (int j = i-1; J >= 0; j--) {if (H[j] <= sum[ I]-sum[j]) {//Current J medium maximum height is greater than or equal to the total book height from J to I, State transfer if (Dp[i] > Dp[j] + i-j-1) { Dp[i] = Dp[j] + i-j-1; Update DP H[i] = sum[i]-sum[j]; Update before I stack book the highest book high height break; }}}} PrinTF ("*case%d:%d\n", ++cnt, Dp[n]); } return 0;} /*65 5 2 3 5 5*/
Nyoj 1216--Finishing Books CF 229d--towers —————— "dp+ greedy"