The problem is basically consistent with POJ 1742: http://www.cnblogs.com/sevenun/p/5442279.html (multiple backpack)
Test instructions: give you n elevators, the first elevator high h[i], the number of c[i], but the height of each elevator can not exceed a[i].
Ask, what kind of construction plan can make the elevator reach the maximum height
Train of thought: first, must make elevator press a[i] to sort, a[i] The smallest elevator builds first. For example, elevator 1, can only be built under the height of 20, and elevator 2 can be built under the height of 50, I would certainly build elevator 1, otherwise if the first build elevator 2, it will lead me to build the height of more than 20, so that can not make full use of the elevator 1.
Dynamic programming, the thought of the transfer equation is d[i][j], the first I elevator construction to the height of J, the number of the first elevator left how many.
The default D[i][j] is-1, which means that the top I elevator cannot reach the height of J.
For D[i][j], if the construction of the former I-1 elevator has been able to reach the height of J, then to the height of J naturally do not need the first elevator, so the remaining c[i] Elevator
If the construction of the former i-1 elevator can not reach the height of J, then I would naturally use the first elevator to see if I can reach the height of J, so d[i][j] = d[i][j-h[i]]-1.
Scrolling array: Since n Max is 400, and A[i] Max is 40000, then by the above definition, then the array is bound to reach 400*40000, feel the memory is not enough
So the observation equation shows that the calculation of d[i][j] only involves the previous row and the current line, so you can use a scrolling array to reduce memory usage.
AC Code:
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int K = 404;const int H = 40005;int n,d[2][h];struct node{int a,c,h;} W[k];int CMP (node n1, node N2) {return n1.a < n2.a;} void Solve () {int f = 1;memset (d,-1, sizeof (d)), sort (w,w+n,cmp), for (int j = 0; j*w[0].h <= w[0].a; j + +) d[0][j*w[0].h] = w[0].c-j;for (int i = 1; i < n; i++) {d[f][0] = w[i].c;for (int j = 1; J <= min (w[i].a, H); j + +) {if (D[!f][j] >= 0 ) D[f][j] = W[i].c;else if (j >= w[i].h) d[f][j] = D[f][j-w[i].h]-1;else D[f][j] = -1;d[!f][j] =-1;} D[!f][0] = -1;f =!f;} int ans = 0;for (int i = w[n-1].a; I >= 0; i--) {ans = i;if (d[!f][i]>= 0) break;} printf ("%d\n", ans);} int main () {//freopen ("In.txt", "R", stdin),//freopen ("OUT.txt", "w", stdout), while (~SCANF ("%d", &n)) {for (int i = 0; I < n; i++) scanf ("%d%d%d", &w[i].h, &W[I].A, &w[i].c); solve ();} return 0;}
Poj 2392 Space Elevator DP